offers wip

This commit is contained in:
ntr committed 2022-08-23 00:02:50 +10:00
1 parent 453c2da045
commit deb9eb3fc2
16 files changed
+276 -88

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
import type Log from 'bunyan';
import type { PrismaClient, Want, User, Listing, Offer, } from '@prisma/client';
import groupBy from 'lodash/groupBy';
import sortBy from 'lodash/sortBy';
import { logAxiosError } from './utils';
import { Pg } from './pg';
export enum Status {
Pending = 'pending',
Accepted = 'accepted',
Ordered = 'ordered',
Delivered = 'delivered',
}
export default function createOffers(log: Log, pg: Pg) {
async function generate(seller: number, bulkShipping: number, baseShipping: number): Offer[] {
log.info('generateOffers', { seller, bulkShipping, baseShipping });
const listings = await pg.listingFindSeller(seller);
log.info('creating offers for listings', { listings })
const shipping = bulkShipping / listings.length;
const offers = listings.map(l => ({
userId: l.want.user.id,
listingId: l.id,
wantId: l.want.id,
status: Status.Pending,
item: l.data.price.value,
shipping,
total: l.data.price.value + shipping,
// without mcrates...
comparison: l.data.price.value + baseShipping,
}))
// we must sort the offers by price,
// sometimes a seller has multiples of a want
// to deal with this we just ignore duplicates on insert
// and ensure the cheapest one goes in first
const sorted = sortBy(offers, ['item']);
const inserts = await pg.offersCreate(sorted);
console.log(inserts);
return inserts;
}
return {
generate,
};
}