Skip to content

Commit

Permalink
Added matcedDonationId to draft donation (#1320)
Browse files Browse the repository at this point in the history
* Added matcedDonationId to draft donation

* Added more condition to test
  • Loading branch information
aminlatifi authored Feb 14, 2024
1 parent cebd57e commit b8e1e95
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddDonationMatchedIdColumnToDraftDonation1707892354691
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE draft_donation ADD COLUMN IF NOT EXISTS "matchedDonationId" integer default NULL`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {}
}
4 changes: 4 additions & 0 deletions src/entities/draftDonation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,8 @@ export class DraftDonation extends BaseEntity {
@Field({ nullable: true })
@Column({ nullable: true })
errorMessage?: string;

@Field()
@Column({ nullable: true })
matchedDonationId?: number;
}
4 changes: 4 additions & 0 deletions src/repositories/draftDonationRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('draftDonationRepository', () => {

it('should mark a draft donation as matched', async () => {
// Setup
const matchedDontionId = 9999;
const draftDonation = await DraftDonation.create({
networkId: 1,
status: DRAFT_DONATION_STATUS.PENDING,
Expand All @@ -33,6 +34,7 @@ describe('draftDonationRepository', () => {
await draftDonation.save();

await markDraftDonationStatusMatched({
matchedDonationId: matchedDontionId,
fromWalletAddress: draftDonation.fromWalletAddress,
toWalletAddress: draftDonation.toWalletAddress,
networkId: draftDonation.networkId,
Expand All @@ -43,10 +45,12 @@ describe('draftDonationRepository', () => {
const updatedDraftDonation = await DraftDonation.findOne({
where: {
id: draftDonation.id,
matchedDonationId: matchedDontionId,
},
});

expect(updatedDraftDonation?.status).equal(DRAFT_DONATION_STATUS.MATCHED);
expect(updatedDraftDonation?.matchedDonationId).equal(matchedDontionId);
});

it('should clear expired draft donations', async () => {
Expand Down
16 changes: 13 additions & 3 deletions src/repositories/draftDonationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ import { logger } from '../utils/logger';

// mark donation status matched based on fromWalletAddress, toWalletAddress, networkId, tokenAddress and amount
export async function markDraftDonationStatusMatched(params: {
matchedDonationId: number;
fromWalletAddress: string;
toWalletAddress: string;
networkId: number;
currency: string;
amount: number;
}): Promise<void> {
try {
const { fromWalletAddress, toWalletAddress, networkId, currency, amount } =
params;
const {
fromWalletAddress,
toWalletAddress,
networkId,
currency,
amount,
matchedDonationId,
} = params;
await DraftDonation.update(
{
fromWalletAddress,
Expand All @@ -24,7 +31,10 @@ export async function markDraftDonationStatusMatched(params: {
amount,
status: DRAFT_DONATION_STATUS.PENDING,
},
{ status: DRAFT_DONATION_STATUS.MATCHED },
{
status: DRAFT_DONATION_STATUS.MATCHED,
matchedDonationId,
},
);
} catch (e) {
logger.error(
Expand Down
5 changes: 5 additions & 0 deletions src/resolvers/donationResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2571,6 +2571,11 @@ function createDonationTestCases() {
},
});
assert.equal(updatedDraftDonation?.status, DRAFT_DONATION_STATUS.MATCHED);
assert.isOk(updatedDraftDonation?.matchedDonationId);
assert.equal(
updatedDraftDonation?.matchedDonationId,
saveDonationResponse.data.data.createDonation,
);
});
}

Expand Down
1 change: 1 addition & 0 deletions src/resolvers/donationResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ export class DonationResolver {
);

await markDraftDonationStatusMatched({
matchedDonationId: donation.id,
fromWalletAddress: fromAddress,
toWalletAddress: toAddress,
currency: token,
Expand Down
5 changes: 3 additions & 2 deletions src/services/chains/evm/draftDonationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,16 @@ async function submitMatchedDraftDonation(
logger.debug(
`Donation with ID ${donationId} has been created for draftDonation with ID ${draftDonation.id}`,
);
draftDonation.status = DRAFT_DONATION_STATUS.MATCHED;
// donation resolver does it
// draftDonation.status = DRAFT_DONATION_STATUS.MATCHED;
// draftDonation.matchedDonationId = Number(donationId);
} catch (e) {
logger.error(
`Error on creating donation for draftDonation with ID ${draftDonation.id}`,
e,
);
draftDonation.status = DRAFT_DONATION_STATUS.FAILED;
draftDonation.errorMessage = e.message;
} finally {
await draftDonation.save();
}
}
Expand Down

0 comments on commit b8e1e95

Please sign in to comment.