non discogs users

This commit is contained in:
ntr committed 2022-08-06 21:49:40 +10:00
1 parent 66dcdaeb70
commit b4114a2440
8 files changed
+93 -49

No files matched your search

+55 -3
View File
@@ -2,13 +2,14 @@ import express from 'express';
import http from 'http';
import cors from 'cors';
import { groupBy } from 'lodash';
import Bluebird from 'bluebird';
import type Log from 'bunyan';
import { Pg } from './pg';
import type { Listing, User, Prisma } from '@prisma/client';
import type { Listing, User, Want, Prisma } from '@prisma/client';
export default function createApi(log: Log, pg: Pg) {
export default function createApi(log: Log, pg: Pg, discogs) {
const PORT = 41337;
const app = express();
@@ -32,7 +33,7 @@ export default function createApi(log: Log, pg: Pg) {
currency: l.data.original_price.curr_abbr,
condition: l.data.condition,
uri: l.data.uri,
user: l.want.user.discogs,
user: l.want.user.name,
}));
const shipping = b[0]?.data.seller.shipping;
@@ -57,6 +58,24 @@ export default function createApi(log: Log, pg: Pg) {
}
async function populateUserListings(user: User) {
log.info(user);
const wants = await discogs.wantlist(user);
// const latest = await discogs.wantlistLatest(user);
// if (wants.length !== latest.length)
log.info({ username: user.discogs, wantCount: wants.length });
return Bluebird.mapSeries<Want, Listing[]>(wants, async want => {
const listings = await discogs.listings(want);
const populated = listings.filter(l => l);
log.info({ id: want.id, release: want.release, populated: populated.length, total: listings.length });
return populated;
});
}
function listen() {
return new Promise((resolve, reject) => {
server.on('error', err => {
@@ -70,6 +89,7 @@ export default function createApi(log: Log, pg: Pg) {
}
app.use(cors());
app.use(express.json())
app.get('/bundles', async (req, res, next) => {
const bundles = await getBundles();
@@ -77,6 +97,38 @@ export default function createApi(log: Log, pg: Pg) {
res.json(bundles);
})
app.get('/user/:id/offers', async function userOffersGet(req, res, next) {
const { id } = req.params;
const user = await pg.userFindId(id);
res.json(user);
});
app.post('/user', async function usersPost(req, res, next) {
const { name } = req.body;
const user = await pg.userUpsert(null, name, null);
res.json(user);
});
app.post('/user/populate', async function usersPopulate(req, res, next) {
const { userId } = req.body;
const user = await pg.userFindId(userId);
const listings = await populateUserListings(user);
res.json(listings);
});
app.post('/want', async function wantPost(req, res, next) {
const { userId, releaseId } = req.body;
const user = await pg.userFindId(userId);
const release = await discogs.release(releaseId);
const want = await pg.wantUpsert(release, user);
return res.json(want);
});
app.use(function errorHandler(req, res, next, error) {
log.warn(error);
res.status(500).send({ error });
});
return {
app,
listen,