From 0cec6414aed382c8c98aed9472b8aa70fb42897f Mon Sep 17 00:00:00 2001 From: ntr Date: Mon, 26 Dec 2022 19:02:26 +1100 Subject: [PATCH] open offers fix --- api/src/pg.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/api/src/pg.ts b/api/src/pg.ts index 0db7412..d2d06c2 100644 --- a/api/src/pg.ts +++ b/api/src/pg.ts @@ -131,8 +131,8 @@ export default function createPg(log: Log, prisma: PrismaClient) { const protectedOfferStatuses = [Status.Confirmed, Status.Complete]; - // can't delete a listing with an open offer - // so delete those offers first + // delete all the offers where we don't want to keep them + // for historical reasons const { count: offersRemoved } = await prisma.offer.deleteMany({ where: { wantId: want.id, @@ -145,7 +145,7 @@ export default function createPg(log: Log, prisma: PrismaClient) { } }); - if (offersRemoved > 0) log.info({ offersRemoved }, 'open offers removed'); + if (offersRemoved > 0) log.info({ offersRemoved }, 'pending offers removed'); // now we have to filter listingIds with confirmed offers // and get their listingIds to filter out @@ -154,23 +154,22 @@ export default function createPg(log: Log, prisma: PrismaClient) { wantId: want.id, status: { in: protectedOfferStatuses, - } + }, }, select: { listingId: true, - } + }, })).map(r => r.listingId); - const removalIds = _.without(currentListingIds, ...confirmedListingIds); + const removalIds = currentListingIds.filter(id => confirmedListingIds.includes(id)); + + if (removalIds.length) log.info({ removalIds }); // finally we delete listings that aren't related // to a historical offer const { count } = await prisma.listing.deleteMany({ where: { - wantId: want.id, - id: { - notIn: currentListingIds, - } - } + id: { in: removalIds }, + }, }); log.info({ count }, 'second stage open offers removed');