offers changes and select status
This commit is contained in:
1 parent
bbc3fe6b48
commit
5f853696ab
10 files changed
+89
-18
No files matched your search
@@ -27,6 +27,7 @@ export default function createEvents(log: Log, pg: Pg, discogs, offers) {
|
||||
await setTimeout(60 * 1000);
|
||||
return populateListings(want);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -10,8 +10,9 @@ import { Pg } from './pg';
|
||||
export enum Status {
|
||||
Pending = 'pending',
|
||||
Accepted = 'accepted',
|
||||
Declined = 'declined',
|
||||
Ordered = 'ordered',
|
||||
Delivered = 'delivered',
|
||||
Complete = 'complete',
|
||||
}
|
||||
|
||||
export default function createOffers(log: Log, pg: Pg) {
|
||||
|
||||
+51
-12
@@ -2,7 +2,7 @@ import bcrypt from 'bcrypt';
|
||||
import type Log from 'bunyan';
|
||||
import type { PrismaClient, Want, User, Listing, Offer, Prisma } from '@prisma/client';
|
||||
import * as Bluebird from 'bluebird';
|
||||
import omit from 'lodash/omit';
|
||||
import _ from 'lodash';
|
||||
|
||||
import { Status } from './offers';
|
||||
|
||||
@@ -123,17 +123,56 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg {
|
||||
});
|
||||
}
|
||||
|
||||
async function listingRmUnlisted(want: Want, ids: DiscogsListing[]) {
|
||||
const { count } = await prisma.listing.deleteMany({
|
||||
where: {
|
||||
wantId: want.id,
|
||||
id: {
|
||||
notIn: ids,
|
||||
}
|
||||
}
|
||||
});
|
||||
async function listingRmUnlisted(want: Want, currentListingIds: DiscogsListing[]) {
|
||||
return prisma.$transaction(async (prisma) => {
|
||||
log.debug({ currentListingIds });
|
||||
|
||||
return count;
|
||||
const openOfferStatuses = [Status.Pending, Status.Accepted];
|
||||
|
||||
// can't delete a listing with an open offer
|
||||
// so delete those offers first
|
||||
const { count: offersRemoved } = await prisma.offer.deleteMany({
|
||||
where: {
|
||||
wantId: want.id,
|
||||
listingId: {
|
||||
notIn: currentListingIds,
|
||||
},
|
||||
status: {
|
||||
in: openOfferStatuses,
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
if (offersRemoved > 0) log.info({ offersRemoved }, 'open offers removed');
|
||||
|
||||
// now we have to filter listingIds with confirmed offers
|
||||
// and get their listingIds to filter out
|
||||
const confirmedListingIds = (await prisma.offer.findMany({
|
||||
where: {
|
||||
wantId: want.id,
|
||||
status: {
|
||||
notIn: openOfferStatuses,
|
||||
}
|
||||
},
|
||||
select: {
|
||||
listingId: true,
|
||||
}
|
||||
})).map(r => r.listingId);
|
||||
const removalIds = _.without(currentListingIds, ...confirmedListingIds);
|
||||
|
||||
// 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,
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return count;
|
||||
});
|
||||
}
|
||||
|
||||
async function listingUpsert(listing, want) {
|
||||
@@ -214,7 +253,7 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg {
|
||||
listingId: offer.listingId,
|
||||
},
|
||||
create: offer,
|
||||
update: omit(offer, ['status']),
|
||||
update: _.omit(offer, ['status']),
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user