From 644398319d206b79b5e40461744910a59d8f5f5e Mon Sep 17 00:00:00 2001 From: ntr Date: Thu, 15 Sep 2022 22:14:54 +1000 Subject: [PATCH] prod nginx changes and remove want on offer complete --- TODO.md | 1 + api/src/offers.ts | 16 ++++++++++++---- api/src/pg.ts | 23 +++++++++++++++++++++++ api/src/utils.ts | 4 +++- client/src/components/header.tsx | 6 +++++- nginx/matescrates.au.nginx.conf | 20 ++++++++------------ 6 files changed, 52 insertions(+), 18 deletions(-) diff --git a/TODO.md b/TODO.md index b935ebe..39984ff 100644 --- a/TODO.md +++ b/TODO.md @@ -2,5 +2,6 @@ push offers cover off admin routes and commands +remove want on offer complete do we keep historical offers / wants? diff --git a/api/src/offers.ts b/api/src/offers.ts index 6d97541..71f0e9a 100644 --- a/api/src/offers.ts +++ b/api/src/offers.ts @@ -5,7 +5,7 @@ import groupBy from 'lodash/groupBy'; import sortBy from 'lodash/sortBy'; import { Context } from 'koa'; -import { logAxiosError } from './utils'; +import { logAxiosError, wantDetails } from './utils'; import { Pg } from './pg'; export enum Status { @@ -65,9 +65,17 @@ export default function createOffers(log: Log, pg: Pg) { } async function statusSet(user: User, id: UUID, status: Status) { - // check the user actually has this offer - const offer = await pg.userOfferFind(user, id); - return pg.offerStatusSet(offer.id, status); + log.info({ status, id }, 'setting offer status'); + + const offer = await pg.offerFindId(id); + const update = await pg.offerStatusSet(offer.id, status); + + if (status === Status.Complete) { + log.info({ id, status }, 'completed offer, removing want...'); + await pg.wantRm(offer.wantId); + } + + return update; } async function bundles() { diff --git a/api/src/pg.ts b/api/src/pg.ts index 2639ea9..4cb4432 100644 --- a/api/src/pg.ts +++ b/api/src/pg.ts @@ -27,12 +27,14 @@ export interface Pg { wantCreate: (discogsWant: any, user: User) => Promise, wantList: (user: User) => Promise, wantsToSync: () => Promise, + wantRm: (id: UUID) => Promise, bundleCandidates: () => Promise, offersCreate: (offers: Offer[]) => Promise, offerStatusSet: (id: UUID, status: Status) => Promise, offersAdmin: () => Promise, + offerFindId: (id: UUID) => Promise, } export default function createPg(log: Log, prisma: PrismaClient): Pg { @@ -60,6 +62,15 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg { }); } + async function offerFindId(id: UUID) { + return prisma.offer.findFirstOrThrow({ + where: { id }, + include: { + want: true, + } + }); + } + async function userOfferFind(user: User, id: UUID) { return prisma.offer.findFirstOrThrow({ where: { @@ -86,6 +97,9 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg { }); } + // userUpdate({ email: 'jen@vdk.com', password: 'jen' }); + // userUpdate({ email: 'james@mcd', password: 'james' }); + async function userUpdate(params: UserCreateParams) { const hashRounds = 10; const password = await bcrypt.hash(params.password, hashRounds); @@ -111,6 +125,12 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg { return prisma.want.findMany({}); } + async function wantRm(id: UUID) { + await prisma.want.delete({ + where: { id }, + }); + } + async function wantCreate(discogsRelease, user) { const data: { userId: UUID, release: DiscogsListing, data: any } = { data: discogsRelease, @@ -286,6 +306,8 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg { wantCreate, wantList, wantsToSync, + wantRm, + listingUpsert, listingFind, listingRmUnlisted, @@ -296,5 +318,6 @@ export default function createPg(log: Log, prisma: PrismaClient): Pg { offersCreate, offerStatusSet, offersAdmin, + offerFindId, }; } \ No newline at end of file diff --git a/api/src/utils.ts b/api/src/utils.ts index 7254e45..8092d17 100644 --- a/api/src/utils.ts +++ b/api/src/utils.ts @@ -23,4 +23,6 @@ export function wantDetails(want: Want) { artist: data.artists_sort, id: want.id } -} \ No newline at end of file +} + +export function offerDetails() {} diff --git a/client/src/components/header.tsx b/client/src/components/header.tsx index 73c6cd4..1708f61 100644 --- a/client/src/components/header.tsx +++ b/client/src/components/header.tsx @@ -5,6 +5,10 @@ import { useSelector, useDispatch } from 'react-redux' import http from './../http'; import { State, userState } from './../store'; +function showAdmin(user) { + return user && user.email === 'ntr@mnml.gg'; +} + const Header = () => { const dispatch = useDispatch() const user = useSelector((state: State) => state.user); @@ -48,7 +52,7 @@ const Header = () => { return (
{user ? nav() : anonNav()} - {user ? adminNav() : null} + {showAdmin(user) ? adminNav() :
} {user ? userNav() : null}
); diff --git a/nginx/matescrates.au.nginx.conf b/nginx/matescrates.au.nginx.conf index 3fad037..3ba33e7 100644 --- a/nginx/matescrates.au.nginx.conf +++ b/nginx/matescrates.au.nginx.conf @@ -6,16 +6,11 @@ upstream mcrates_client { server client:8080; } -# upstream mcrates_ws { -# server 127.0.0.1:40055; -# } - map $http_upgrade $connection_upgrade { default upgrade; '' close; } -# DEV server { server_name matescrates.au; @@ -30,13 +25,14 @@ server { proxy_read_timeout 600s; } - # location /api/ws { - # proxy_pass http://mcrates_ws; - # proxy_http_version 1.1; - # proxy_set_header Upgrade $http_upgrade; - # proxy_set_header Connection $connection_upgrade; - # proxy_read_timeout 600s; - # } + location /api/ws { + proxy_pass http://mcrates_api; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + proxy_set_header Host $host; + proxy_read_timeout 600s; + } location /api/ { # trailing slash means api will receive url WITHOUT /api