acp stuff
This commit is contained in:
1 parent
deb9eb3fc2
commit
047f606e83
14 files changed
+368
-44
No files matched your search
+20
-5
@@ -2,6 +2,7 @@ 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 { Context } from 'koa';
|
||||
|
||||
import { logAxiosError } from './utils';
|
||||
import { Pg } from './pg';
|
||||
@@ -14,18 +15,22 @@ export enum Status {
|
||||
}
|
||||
|
||||
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);
|
||||
async function generate(listingIds: DiscogsListing[], bulkShipping: number, baseShipping: number) {
|
||||
log.info('generateOffers', { listingIds, bulkShipping, baseShipping });
|
||||
const listings = await pg.listingByIds(listingIds);
|
||||
|
||||
log.info('creating offers for listings', { listings })
|
||||
log.info({ listings }, 'creating offers for listings')
|
||||
|
||||
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,
|
||||
|
||||
item: l.data.price.value,
|
||||
@@ -42,11 +47,21 @@ export default function createOffers(log: Log, pg: Pg) {
|
||||
// and ensure the cheapest one goes in first
|
||||
const sorted = sortBy(offers, ['item']);
|
||||
const inserts = await pg.offersCreate(sorted);
|
||||
console.log(inserts);
|
||||
|
||||
return inserts;
|
||||
}
|
||||
|
||||
async function accept(ctx: Context) {
|
||||
const { id } = ctx.request.body;
|
||||
ctx.assert(id, 401, 'no id supplied');
|
||||
|
||||
// check the user actually has this offer
|
||||
const offer = await pg.userOfferFind(ctx.state.user, id);
|
||||
return pg.offerSetStatus(offer.id, Status.Accepted);
|
||||
}
|
||||
|
||||
return {
|
||||
generate,
|
||||
accept,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user