want events

This commit is contained in:
ntr committed 2022-08-17 23:22:48 +10:00
1 parent f5e75f4fe6
commit 30bf7426e4
19 files changed
+293 -108

No files matched your search

+10 -26
View File
@@ -31,30 +31,15 @@ export default function discogs(log: Log, pg: Pg) {
return null;
}
// async function user(discogs: string) {
// const user = await pg.userFindName(discogs);
// if (user) return user;
// log.debug('fetching user', discogs);
// const { data } = await api.get(`${apiBase}/users/${discogs}`);
// return pg.userUpsert(data.username, data.username, data);
// }
async function wantlist(user: User): Promise<Want[]> {
const stored = await pg.userWants(user);
if (stored.length) return stored;
if (!user.discogs) {
log.error(user, 'attempted to fetch wantlist of non discogs user');
throw new Error('User is not associated with a discogs account');
}
return wantlistLatest(user);
}
async function wantlistLatest(user: User): Promise<Want[]> {
const { username } = user.data as { username: string };
async function wantListSync(username: string, user: User): Promise<Want[]> {
const { data: { wants, pagination }} = await api.get(`${apiBase}/users/${username}/wants`);
return Bluebird.mapSeries(wants, async r => pg.wantUpsert(r, user));
const releases = await Promise.all(wants.map(want => release(want.id)));
// convert want object to actual release and ignore duplicate errors
const results = await Promise.allSettled(releases.map(release => pg.wantCreate(release, user)));
// @ts-ignore
const additions = results.filter(r => r.status === 'fulfilled').map(r => r.value);
return additions;
}
async function release(id: DiscogsRelease): Promise<any> {
@@ -99,6 +84,7 @@ export default function discogs(log: Log, pg: Pg) {
try {
log.debug('fetching listing', id);
const res = await api.get(`${apiBase}/marketplace/listings/${id}`);
log.info(res.data);
return pg.listingUpsert(res.data, want);
} catch (e) {
return handleApiError(e);
@@ -106,9 +92,7 @@ export default function discogs(log: Log, pg: Pg) {
}
return {
// user,
wantlist,
wantlistLatest,
wantListSync,
inventory,
listings,
listing,