offers wip
This commit is contained in:
1 parent
453c2da045
commit
deb9eb3fc2
16 files changed
+276
-88
No files matched your search
+49
-4
@@ -1,6 +1,6 @@
|
||||
import bcrypt from 'bcrypt';
|
||||
import type Log from 'bunyan';
|
||||
import type { PrismaClient, Want, User, Listing, Prisma } from '@prisma/client';
|
||||
import type { PrismaClient, Want, User, Listing, Offer, Prisma } from '@prisma/client';
|
||||
import * as Bluebird from 'bluebird';
|
||||
|
||||
export interface UserCreateParams {
|
||||
@@ -13,15 +13,20 @@ export interface Pg {
|
||||
userFindEmail: (email: Email) => Promise<User>,
|
||||
userCreate: (params: UserCreateParams) => Promise<User>,
|
||||
userWants: (user: User) => Promise<Want[]>,
|
||||
userOffers: (user: User) => Promise<Offer[]>,
|
||||
|
||||
listingUpsert: (listing: Listing, want: Want) => Promise<Listing>,
|
||||
listingFind: (id: DiscogsListing) => Promise<Listing>,
|
||||
listingFindSeller: (id: DiscogsSeller) => Promise<Listing[]>,
|
||||
listingRmUnlisted: (want: Want, ids: DiscogsListing[]) => Promise<number>,
|
||||
|
||||
wantCreate: (discogsWant: any, user: User) => Promise<Want>,
|
||||
wantList: (user: User) => Promise<Want[]>,
|
||||
wantsToSync: () => Promise<Want[]>,
|
||||
listingUpsert: (listing: Listing, want: Want) => Promise<Listing>,
|
||||
listingFind: (id: DiscogsListing) => Promise<Listing>,
|
||||
listingRmUnlisted: (want: Want, ids: DiscogsListing[]) => Promise<number>,
|
||||
|
||||
bundleCandidates: () => Promise<Listing[][]>,
|
||||
|
||||
offersCreate: (offers: Offer[]) => Promise<Offer[]>,
|
||||
}
|
||||
|
||||
export default function createPg(log: Log, prisma: PrismaClient): Pg {
|
||||
@@ -37,6 +42,15 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg {
|
||||
return prisma.want.findMany({ where: { userId: user.id }});
|
||||
}
|
||||
|
||||
async function userOffers(user: User) {
|
||||
return prisma.offer.findMany({
|
||||
where: { userId: user.id },
|
||||
include: {
|
||||
want: true,
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function userCreate(params: UserCreateParams) {
|
||||
const hashRounds = 10;
|
||||
const password = await bcrypt.hash(params.password, hashRounds);
|
||||
@@ -131,6 +145,19 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg {
|
||||
});
|
||||
}
|
||||
|
||||
async function listingFindSeller(seller: DiscogsSeller): Promise<Listing[]> {
|
||||
return prisma.listing.findMany({
|
||||
where: {
|
||||
seller,
|
||||
},
|
||||
include: {
|
||||
want: {
|
||||
include: { user: true },
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function bundleCandidates(): Promise<Listing[][]> {
|
||||
const candidates = await prisma.$queryRaw`
|
||||
select
|
||||
@@ -157,11 +184,27 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg {
|
||||
})));
|
||||
}
|
||||
|
||||
async function offersCreate(offers: Offer[]) {
|
||||
return await prisma.$transaction(
|
||||
offers.map(offer => prisma.offer.upsert({
|
||||
where: {
|
||||
userId_wantId: {
|
||||
userId: offer.userId,
|
||||
wantId: offer.wantId,
|
||||
},
|
||||
},
|
||||
create: offer,
|
||||
update: offer
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
userFindId,
|
||||
userFindEmail,
|
||||
userCreate,
|
||||
userWants,
|
||||
userOffers,
|
||||
|
||||
wantCreate,
|
||||
wantList,
|
||||
@@ -169,7 +212,9 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg {
|
||||
listingUpsert,
|
||||
listingFind,
|
||||
listingRmUnlisted,
|
||||
listingFindSeller,
|
||||
|
||||
bundleCandidates,
|
||||
offersCreate,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user