nice bundles markup
This commit is contained in:
1 parent
02aadcf2db
commit
413c1f4840
9 files changed
+301
-41
No files matched your search
Generated
+13
@@ -19,6 +19,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bluebird": "^3.5.36",
|
||||
"@types/lodash": "^4.14.182",
|
||||
"node-dev": "^7.4.3",
|
||||
"prisma": "^4.1.0",
|
||||
"ts-node-dev": "^2.0.0",
|
||||
@@ -124,6 +125,12 @@
|
||||
"integrity": "sha512-HBNx4lhkxN7bx6P0++W8E289foSu8kO8GCk2unhuVggO+cE7rh9DhZUyPhUxNRG9m+5B5BTKxZQ5ZP92x/mx9Q==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/lodash": {
|
||||
"version": "4.14.182",
|
||||
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.182.tgz",
|
||||
"integrity": "sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@types/node": {
|
||||
"version": "18.0.6",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.0.6.tgz",
|
||||
@@ -1790,6 +1797,12 @@
|
||||
"integrity": "sha512-HBNx4lhkxN7bx6P0++W8E289foSu8kO8GCk2unhuVggO+cE7rh9DhZUyPhUxNRG9m+5B5BTKxZQ5ZP92x/mx9Q==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/lodash": {
|
||||
"version": "4.14.182",
|
||||
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.182.tgz",
|
||||
"integrity": "sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==",
|
||||
"dev": true
|
||||
},
|
||||
"@types/node": {
|
||||
"version": "18.0.6",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-18.0.6.tgz",
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/bluebird": "^3.5.36",
|
||||
"@types/lodash": "^4.14.182",
|
||||
"node-dev": "^7.4.3",
|
||||
"prisma": "^4.1.0",
|
||||
"ts-node-dev": "^2.0.0",
|
||||
|
||||
+38
-21
@@ -1,10 +1,12 @@
|
||||
import express from 'express';
|
||||
import http from 'http';
|
||||
import cors from 'cors';
|
||||
import { groupBy } from 'lodash';
|
||||
|
||||
import type Log from 'bunyan';
|
||||
|
||||
import { Pg } from './pg';
|
||||
import type { Listing, User, Prisma } from '@prisma/client';
|
||||
|
||||
export default function createApi(log: Log, pg: Pg) {
|
||||
const PORT = 41337;
|
||||
@@ -12,39 +14,53 @@ export default function createApi(log: Log, pg: Pg) {
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
|
||||
interface ListingData extends Listing {
|
||||
data: any,
|
||||
want: { user: User },
|
||||
};
|
||||
|
||||
async function getBundles() {
|
||||
const bundles = await pg.bundleCandidates();
|
||||
|
||||
bundles.forEach(b => {
|
||||
const data = b.map(l => l.data) as any[];
|
||||
const wants = b.map(l => l.wantId);
|
||||
const total = data.reduce((acc, l) => acc + l.price.value, 0);
|
||||
const items = data.map(l => ({
|
||||
id: l.id,
|
||||
description: l.release.description,
|
||||
price: l.price.value,
|
||||
condition: l.condition,
|
||||
uri: l.uri,
|
||||
return bundles.map((b: ListingData[]) => {
|
||||
const total = b.reduce((acc, l) => acc + l.data.original_price.value, 0);
|
||||
|
||||
const items = b.map(l => ({
|
||||
id: l.data.id,
|
||||
description: l.data.release.description,
|
||||
price: l.data.original_price.value,
|
||||
currency: l.data.original_price.curr_abbr,
|
||||
condition: l.data.condition,
|
||||
uri: l.data.uri,
|
||||
user: l.want.user.discogs,
|
||||
}));
|
||||
|
||||
const username = data[0]?.seller.username
|
||||
const seller = data[0]?.seller.id
|
||||
const shipsFrom = data[0]?.ships_from;
|
||||
const currency = data[0]?.price.currency;
|
||||
const shipping = data[0]?.seller.shipping;
|
||||
const mentionsAustralia = shipping.toLowerCase().includes('australia');
|
||||
const mentionsWorldwide = shipping.toLowerCase().includes('world');
|
||||
log.info({ count: b.length, currency, total, items, username, seller, shipsFrom, mentionsAustralia, mentionsWorldwide });
|
||||
});
|
||||
const shipping = b[0]?.data.seller.shipping;
|
||||
|
||||
return bundles;
|
||||
console.log(b.map(l => l.data));
|
||||
|
||||
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.original_price.curr_abbr,
|
||||
shipping: '...',
|
||||
items,
|
||||
mentionsAustralia: shipping.toLowerCase().includes('australia'),
|
||||
mentionsWorldwide: shipping.toLowerCase().includes('world'),
|
||||
}
|
||||
|
||||
log.info(bundle);
|
||||
return bundle;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function listen() {
|
||||
return new Promise((resolve, reject) => {
|
||||
server.on('error', err => {
|
||||
this.reject(err);
|
||||
reject(err);
|
||||
})
|
||||
|
||||
server.listen(PORT, () => {
|
||||
@@ -57,6 +73,7 @@ export default function createApi(log: Log, pg: Pg) {
|
||||
|
||||
app.get('/bundles', async (req, res, next) => {
|
||||
const bundles = await getBundles();
|
||||
// log.info(bundles);
|
||||
res.json(bundles);
|
||||
})
|
||||
|
||||
|
||||
@@ -97,6 +97,11 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg {
|
||||
return prisma.listing.findUnique({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
include: {
|
||||
want: {
|
||||
include: { user: true },
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -118,6 +123,11 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg {
|
||||
where: {
|
||||
id: { in: c.listings }
|
||||
},
|
||||
include: {
|
||||
want: {
|
||||
include: { user: true },
|
||||
}
|
||||
},
|
||||
distinct: ['wantId'],
|
||||
})));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user