completely ditch this bundles intermediate data type
This commit is contained in:
1 parent
6d18a86697
commit
28759f9e70
4 files changed
+16
-72
No files matched your search
@@ -68,10 +68,8 @@ model Offer {
|
||||
condition String @default("")
|
||||
|
||||
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])
|
||||
|
||||
+2
-11
@@ -61,17 +61,8 @@ export default function createEvents(log: Log, pg: Pg, discogs, offers) {
|
||||
});
|
||||
|
||||
events.on(Events.ListingsUpdated, async function listingsUpdated() {
|
||||
const bundles = await offers.bundles();
|
||||
log.info({ event: Events.ListingsUpdated, bundles: bundles.length });
|
||||
|
||||
for (const bundle of bundles) {
|
||||
log.debug({ seller: bundle.seller, items: bundle.items.length }, 'generating offers...');
|
||||
const listingIds = bundle.items.map(i => i.id);
|
||||
const updatedOffers = await offers.generate(listingIds, 100, 30)
|
||||
log.debug({ offers: updatedOffers.length }, 'offers set');
|
||||
}
|
||||
|
||||
log.trace('offers up to date');
|
||||
const updatedOffers = await offers.generate()
|
||||
log.debug({ offers: updatedOffers.length }, 'offers up to date');
|
||||
});
|
||||
|
||||
events.on(Events.Connection, async function onConnection({ ws, id }) {
|
||||
|
||||
+6
-55
@@ -3,6 +3,7 @@ import type { PrismaClient, Want, User, Listing, Offer, } from '@prisma/client';
|
||||
import assert from 'assert/strict';
|
||||
import groupBy from 'lodash/groupBy';
|
||||
import sortBy from 'lodash/sortBy';
|
||||
import flatMap from 'lodash/flatMap';
|
||||
import { Context } from 'koa';
|
||||
|
||||
import { logAxiosError, wantDetails } from './utils';
|
||||
@@ -21,24 +22,17 @@ function sortOffers(o) {
|
||||
}
|
||||
|
||||
export default function createOffers(log: Log, pg: Pg) {
|
||||
async function generate(listingIds: DiscogsListing[], bulkShipping: number, baseShipping: number) {
|
||||
log.debug('generateOffers', { listingIds, bulkShipping, baseShipping });
|
||||
const listings = await pg.listingByIds(listingIds);
|
||||
|
||||
// log.info({ listings }, 'creating offers for listings')
|
||||
async function generate() {
|
||||
const listings = flatMap(await pg.interestingListings());
|
||||
log.debug('generateOffers', { listings: listings.length });
|
||||
|
||||
const shipping = 7.5; // yolt
|
||||
|
||||
// const shipping = bulkShipping / listings.length;
|
||||
// how to deal with users who don't want all items in a bundle...
|
||||
|
||||
const offers = listings.map(l => ({
|
||||
userId: l.want.user.id,
|
||||
listingId: l.id,
|
||||
wantId: l.want.id,
|
||||
|
||||
// sellerId: l.seller,
|
||||
|
||||
status: Status.Pending,
|
||||
|
||||
condition: l.data.condition,
|
||||
@@ -46,9 +40,6 @@ export default function createOffers(log: Log, pg: Pg) {
|
||||
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,
|
||||
@@ -56,7 +47,8 @@ export default function createOffers(log: Log, pg: Pg) {
|
||||
// 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);
|
||||
|
||||
const inserts = await pg.offersUpdate(sorted);
|
||||
|
||||
return inserts;
|
||||
}
|
||||
@@ -84,46 +76,6 @@ export default function createOffers(log: Log, pg: Pg) {
|
||||
return update;
|
||||
}
|
||||
|
||||
async function bundles() {
|
||||
const bundles = await pg.bundleCandidates();
|
||||
|
||||
interface ListingData extends Listing {
|
||||
data: any,
|
||||
want: { user: User },
|
||||
};
|
||||
|
||||
return bundles.map((b: ListingData[]) => {
|
||||
const total = b.reduce((acc, l) => acc + l.data.price.value, 0);
|
||||
const items = b.map(l => ({
|
||||
id: l.data.id,
|
||||
description: l.data.release.description,
|
||||
originalPrice: l.data.original_price.value,
|
||||
originalCurrency: l.data.original_price.curr_abbr,
|
||||
price: l.data.price.value,
|
||||
currency: l.data.price.currency,
|
||||
condition: l.data.condition,
|
||||
uri: l.data.uri,
|
||||
user: l.want.user,
|
||||
}));
|
||||
|
||||
const shipping = b[0]?.data.seller.shipping;
|
||||
|
||||
const bundle = {
|
||||
username: b[0]?.data.seller.username,
|
||||
seller: b[0]?.data.seller.id,
|
||||
shipsFrom: b[0]?.data.ships_from,
|
||||
total,
|
||||
currency: b[0]?.data.price.currency,
|
||||
shipping: '...',
|
||||
items,
|
||||
mentionsAustralia: shipping.toLowerCase().includes('australia'),
|
||||
mentionsWorldwide: shipping.toLowerCase().includes('world'),
|
||||
}
|
||||
|
||||
return bundle;
|
||||
});
|
||||
}
|
||||
|
||||
async function acpOffers() {
|
||||
const offers = await pg.offersAdmin();
|
||||
|
||||
@@ -168,7 +120,6 @@ export default function createOffers(log: Log, pg: Pg) {
|
||||
return {
|
||||
generate,
|
||||
accept,
|
||||
bundles,
|
||||
statusSet,
|
||||
acpOffers,
|
||||
userOffers,
|
||||
|
||||
+8
-4
@@ -216,7 +216,10 @@ export default function createPg(log: Log, prisma: PrismaClient) {
|
||||
});
|
||||
}
|
||||
|
||||
async function bundleCandidates(): Promise<Listing[][]> {
|
||||
async function interestingListings() {
|
||||
// this basically just needs to return the listingIds to generate offers
|
||||
// but for ease of abstraction we get the listings themselves
|
||||
// grouped by seller
|
||||
const candidates = await prisma.$queryRaw`
|
||||
select
|
||||
seller, count(distinct("wantId")), array_agg(id) as listings
|
||||
@@ -244,7 +247,7 @@ export default function createPg(log: Log, prisma: PrismaClient) {
|
||||
})));
|
||||
}
|
||||
|
||||
async function offersCreate(offers: Offer[]) {
|
||||
async function offersUpdate(offers: Offer[]) {
|
||||
return await prisma.$transaction(
|
||||
offers.map(offer => prisma.offer.upsert({
|
||||
where: {
|
||||
@@ -263,6 +266,7 @@ export default function createPg(log: Log, prisma: PrismaClient) {
|
||||
});
|
||||
}
|
||||
|
||||
// effectively all offers
|
||||
async function offersAdmin() {
|
||||
return prisma.offer.findMany({
|
||||
include: {
|
||||
@@ -291,9 +295,9 @@ export default function createPg(log: Log, prisma: PrismaClient) {
|
||||
listingRmUnlisted,
|
||||
listingByIds,
|
||||
|
||||
bundleCandidates,
|
||||
interestingListings,
|
||||
|
||||
offersCreate,
|
||||
offersUpdate,
|
||||
offerStatusSet,
|
||||
offersAdmin,
|
||||
offerFindId,
|
||||
|
||||
Reference in new issue
Block a user