|
| 1 | +import { ActionHandler } from '~actions/types'; |
| 2 | +import { mutate } from '~amplifyClient'; |
| 3 | +import { |
| 4 | + ColonyActionType, |
| 5 | + CreateExpenditureDocument, |
| 6 | + CreateExpenditureMutation, |
| 7 | + CreateExpenditureMutationVariables, |
| 8 | + ExpenditureStatus, |
| 9 | + ExpenditureType, |
| 10 | + NotificationType, |
| 11 | +} from '~graphql'; |
| 12 | +// @TODO: Move closer to the handler |
| 13 | +import { getExpenditure } from '~handlers/expenditures/helpers'; |
| 14 | +import { |
| 15 | + getDomainDatabaseId, |
| 16 | + getExpenditureDatabaseId, |
| 17 | + output, |
| 18 | + toNumber, |
| 19 | + verbose, |
| 20 | + writeActionFromEvent, |
| 21 | +} from '~utils'; |
| 22 | + |
| 23 | +import { sendExpenditureUpdateNotifications } from '~utils/notifications'; |
| 24 | + |
| 25 | +export const handleExpenditureAddedAction: ActionHandler = async (events) => { |
| 26 | + const { contractAddress: colonyAddress, transactionHash } = events[0]; |
| 27 | + const { agent: ownerAddress, expenditureId } = events[0].args; |
| 28 | + const convertedExpenditureId = toNumber(expenditureId); |
| 29 | + |
| 30 | + const expenditure = await getExpenditure( |
| 31 | + colonyAddress, |
| 32 | + convertedExpenditureId, |
| 33 | + ); |
| 34 | + if (!expenditure) { |
| 35 | + output( |
| 36 | + `Could not get expenditure with ID ${convertedExpenditureId} in colony ${colonyAddress}`, |
| 37 | + ); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + const domainId = toNumber(expenditure.domainId); |
| 42 | + const fundingPotId = toNumber(expenditure.fundingPotId); |
| 43 | + |
| 44 | + verbose( |
| 45 | + 'Expenditure with ID', |
| 46 | + convertedExpenditureId, |
| 47 | + 'added in Colony:', |
| 48 | + colonyAddress, |
| 49 | + ); |
| 50 | + |
| 51 | + const databaseId = getExpenditureDatabaseId( |
| 52 | + colonyAddress, |
| 53 | + convertedExpenditureId, |
| 54 | + ); |
| 55 | + |
| 56 | + await mutate<CreateExpenditureMutation, CreateExpenditureMutationVariables>( |
| 57 | + CreateExpenditureDocument, |
| 58 | + { |
| 59 | + input: { |
| 60 | + id: databaseId, |
| 61 | + type: ExpenditureType.PaymentBuilder, |
| 62 | + colonyId: colonyAddress, |
| 63 | + nativeId: convertedExpenditureId, |
| 64 | + ownerAddress, |
| 65 | + status: ExpenditureStatus.Draft, |
| 66 | + slots: [], |
| 67 | + nativeFundingPotId: fundingPotId, |
| 68 | + nativeDomainId: domainId, |
| 69 | + isStaked: false, |
| 70 | + balances: [], |
| 71 | + }, |
| 72 | + }, |
| 73 | + ); |
| 74 | + |
| 75 | + await writeActionFromEvent(events[0], colonyAddress, { |
| 76 | + type: ColonyActionType.CreateExpenditure, |
| 77 | + initiatorAddress: ownerAddress, |
| 78 | + expenditureId: databaseId, |
| 79 | + fromDomainId: getDomainDatabaseId(colonyAddress, domainId), |
| 80 | + }); |
| 81 | + |
| 82 | + sendExpenditureUpdateNotifications({ |
| 83 | + colonyAddress, |
| 84 | + creator: ownerAddress, |
| 85 | + notificationType: NotificationType.ExpenditureReadyForReview, |
| 86 | + transactionHash, |
| 87 | + expenditureID: databaseId, |
| 88 | + }); |
| 89 | +}; |
0 commit comments