offers changes and select status

This commit is contained in:
ntr committed 2022-09-10 17:47:18 +10:00
1 parent bbc3fe6b48
commit 5f853696ab
10 files changed
+89 -18

No files matched your search

+3 -2
View File
@@ -18,7 +18,7 @@ model Listing {
wantId String @db.Uuid
want Want @relation(fields: [wantId], references: [id], onDelete: Cascade, onUpdate: Cascade)
offers Offer[]
offers Offer[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@ -67,12 +67,13 @@ model Offer {
status String
item Decimal @db.Money
// markup Decimal @db.Money
shipping Decimal @db.Money
total Decimal @db.Money
comparison Decimal @db.Money
listingId Int
listing Listing @relation(fields: [listingId], references: [id], onDelete: Restrict)
listing Listing @relation(fields: [listingId], references: [id])
userId String @db.Uuid
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
+1
View File
@@ -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
View File
@@ -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
View File
@@ -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']),
}))
);
}