env files, fixes for discogs sync

This commit is contained in:
ntr committed 2023-02-07 18:23:54 +11:00
1 parent 1ee8dbc749
commit 1ca9a7225c
6 files changed
+31 -14

No files matched your search

+3 -1
View File
@@ -1,3 +1,5 @@
node_modules/ node_modules/
data/ data/
db/ db/
.env
-8
View File
@@ -48,12 +48,4 @@ describe('api', function apiTests() {
assert(res.headers['set-cookie']); assert(res.headers['set-cookie']);
}) })
}); });
describe('offers', function offerTests() {
it('create offers', async function createOffersTest() {
const { data: bundles } = await api.get('/bundles');
const listingIds = bundles[0].items.map(i => i.id);
const { data } = await api.post('/offers', { listingIds, shipping: 50.0, baseShipping: 15.0 });
});
});
}); });
+6 -1
View File
@@ -29,6 +29,10 @@ export default function createApi(log: Log, pg: Pg, discogs: Discogs, offers: Of
const app = koaWebsocket(new koa()); const app = koaWebsocket(new koa());
BigInt.prototype.toJSON = function bigIntStringify() {
return this.toString();
};
app.use(async (ctx, next) => { app.use(async (ctx, next) => {
try { try {
await next(); await next();
@@ -46,6 +50,7 @@ export default function createApi(log: Log, pg: Pg, discogs: Discogs, offers: Of
function listen() { function listen() {
const server = app.listen(PORT); const server = app.listen(PORT);
log.info({ PORT }, 'listening'); log.info({ PORT }, 'listening');
return server;
} }
app.use(cors({ credentials: true })); app.use(cors({ credentials: true }));
@@ -68,7 +73,7 @@ export default function createApi(log: Log, pg: Pg, discogs: Discogs, offers: Of
const { username } = ctx.request.body; const { username } = ctx.request.body;
const user = await pg.userFindId(ctx.state.user.id); const user = await pg.userFindId(ctx.state.user.id);
const wants = await discogs.wantListSync(username, user); const wants = await discogs.wantListSync(username, user);
events.emit(Events.WantsAdded, [wants]); events.emit(Events.WantsAdded, wants);
ctx.response.body = wants; ctx.response.body = wants;
}); });
+16
View File
@@ -27,6 +27,21 @@ export default function createDiscogs(log: Log, pg: Pg) {
}, },
}); });
function onSuccess(response) {
return response;
}
function onError(error) {
// if (error.response) return [error.response.data.error, null];
// if (error.request) return ['Network Error', null];
// return ['Unknown Error', null];
if (error.response.status === 429) {
throw new Error('rate limited, try again in 1 minute');
}
}
api.interceptors.response.use(onSuccess, onError);
async function handleApiError(e) { async function handleApiError(e) {
logAxiosError(e); logAxiosError(e);
if (e.response.status === 429) { if (e.response.status === 429) {
@@ -45,6 +60,7 @@ export default function createDiscogs(log: Log, pg: Pg) {
// convert want object to actual release and ignore duplicate errors // convert want object to actual release and ignore duplicate errors
const results = await Promise.allSettled(releases.map(release => pg.wantCreate(release, user))); const results = await Promise.allSettled(releases.map(release => pg.wantCreate(release, user)));
log.info(results);
// @ts-ignore // @ts-ignore
const additions = results.filter(r => r.status === 'fulfilled').map(r => r.value); const additions = results.filter(r => r.status === 'fulfilled').map(r => r.value);
+2
View File
@@ -17,6 +17,8 @@ export function logAxiosError(e) {
export function wantDetails(want: Want) { export function wantDetails(want: Want) {
const data = want.data as any; const data = want.data as any;
if (!data) console.warn(want);
return { return {
title: data.title, title: data.title,
release: data.id, release: data.id,
+4 -4
View File
@@ -4,9 +4,9 @@ services:
pg: pg:
image: postgres:latest image: postgres:latest
environment: environment:
POSTGRES_USER: 'mcrates' POSTGRES_USER: '${POSTGRES_USER}'
POSTGRES_DB: 'mcrates' POSTGRES_DB: '${POSTGRES_DB}'
POSTGRES_PASSWORD: 'digdeep' POSTGRES_PASSWORD: '${POSTGRES_PASSWORD}'
ports: ports:
- '45432:5432' - '45432:5432'
volumes: volumes:
@@ -19,7 +19,7 @@ services:
- 'pg' - 'pg'
entrypoint: npm start entrypoint: npm start
environment: environment:
DATABASE_URL: 'postgresql://mcrates:digdeep@pg:5432/mcrates?schema=public' DATABASE_URL: 'postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@pg:5432/${POSTGRES_DB}?schema=public'
volumes: volumes:
- "./api:/opt/mcrates/api" - "./api:/opt/mcrates/api"