completely ditch this bundles intermediate data type

This commit is contained in:
ntr committed 2022-11-10 19:46:53 +11:00
1 parent 6d18a86697
commit 28759f9e70
4 files changed
+16 -72

No files matched your search

-2
View File
@@ -68,10 +68,8 @@ model Offer {
condition String @default("") condition String @default("")
item Decimal @db.Money item Decimal @db.Money
// markup Decimal @db.Money
shipping Decimal @db.Money shipping Decimal @db.Money
total Decimal @db.Money total Decimal @db.Money
comparison Decimal @db.Money
listingId Int listingId Int
listing Listing @relation(fields: [listingId], references: [id]) listing Listing @relation(fields: [listingId], references: [id])
+2 -11
View File
@@ -61,17 +61,8 @@ export default function createEvents(log: Log, pg: Pg, discogs, offers) {
}); });
events.on(Events.ListingsUpdated, async function listingsUpdated() { events.on(Events.ListingsUpdated, async function listingsUpdated() {
const bundles = await offers.bundles(); const updatedOffers = await offers.generate()
log.info({ event: Events.ListingsUpdated, bundles: bundles.length }); log.debug({ offers: updatedOffers.length }, 'offers up to date');
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');
}); });
events.on(Events.Connection, async function onConnection({ ws, id }) { events.on(Events.Connection, async function onConnection({ ws, id }) {
+6 -55
View File
@@ -3,6 +3,7 @@ import type { PrismaClient, Want, User, Listing, Offer, } from '@prisma/client';
import assert from 'assert/strict'; import assert from 'assert/strict';
import groupBy from 'lodash/groupBy'; import groupBy from 'lodash/groupBy';
import sortBy from 'lodash/sortBy'; import sortBy from 'lodash/sortBy';
import flatMap from 'lodash/flatMap';
import { Context } from 'koa'; import { Context } from 'koa';
import { logAxiosError, wantDetails } from './utils'; import { logAxiosError, wantDetails } from './utils';
@@ -21,24 +22,17 @@ function sortOffers(o) {
} }
export default function createOffers(log: Log, pg: Pg) { export default function createOffers(log: Log, pg: Pg) {
async function generate(listingIds: DiscogsListing[], bulkShipping: number, baseShipping: number) { async function generate() {
log.debug('generateOffers', { listingIds, bulkShipping, baseShipping }); const listings = flatMap(await pg.interestingListings());
const listings = await pg.listingByIds(listingIds); log.debug('generateOffers', { listings: listings.length });
// log.info({ listings }, 'creating offers for listings')
const shipping = 7.5; // yolt 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 => ({ const offers = listings.map(l => ({
userId: l.want.user.id, userId: l.want.user.id,
listingId: l.id, listingId: l.id,
wantId: l.want.id, wantId: l.want.id,
// sellerId: l.seller,
status: Status.Pending, status: Status.Pending,
condition: l.data.condition, condition: l.data.condition,
@@ -46,9 +40,6 @@ export default function createOffers(log: Log, pg: Pg) {
item: l.data.price.value, item: l.data.price.value,
shipping, shipping,
total: 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, // 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 // to deal with this we just ignore duplicates on insert
// and ensure the cheapest one goes in first // and ensure the cheapest one goes in first
const sorted = sortBy(offers, ['item']); const sorted = sortBy(offers, ['item']);
const inserts = await pg.offersCreate(sorted);
const inserts = await pg.offersUpdate(sorted);
return inserts; return inserts;
} }
@@ -84,46 +76,6 @@ export default function createOffers(log: Log, pg: Pg) {
return update; 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() { async function acpOffers() {
const offers = await pg.offersAdmin(); const offers = await pg.offersAdmin();
@@ -168,7 +120,6 @@ export default function createOffers(log: Log, pg: Pg) {
return { return {
generate, generate,
accept, accept,
bundles,
statusSet, statusSet,
acpOffers, acpOffers,
userOffers, userOffers,
+8 -4
View File
@@ -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` const candidates = await prisma.$queryRaw`
select select
seller, count(distinct("wantId")), array_agg(id) as listings 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( return await prisma.$transaction(
offers.map(offer => prisma.offer.upsert({ offers.map(offer => prisma.offer.upsert({
where: { where: {
@@ -263,6 +266,7 @@ export default function createPg(log: Log, prisma: PrismaClient) {
}); });
} }
// effectively all offers
async function offersAdmin() { async function offersAdmin() {
return prisma.offer.findMany({ return prisma.offer.findMany({
include: { include: {
@@ -291,9 +295,9 @@ export default function createPg(log: Log, prisma: PrismaClient) {
listingRmUnlisted, listingRmUnlisted,
listingByIds, listingByIds,
bundleCandidates, interestingListings,
offersCreate, offersUpdate,
offerStatusSet, offerStatusSet,
offersAdmin, offersAdmin,
offerFindId, offerFindId,