From fbacf1ac63b78e7fea82c76319fc0f2d4ecab392 Mon Sep 17 00:00:00 2001 From: keyan Date: Sun, 10 Dec 2023 17:42:30 -0600 Subject: [PATCH] fix changing subs with different fees --- api/resolvers/item.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/api/resolvers/item.js b/api/resolvers/item.js index 779330af..f936c3f7 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -1055,10 +1055,20 @@ export const createMentions = async (item, models) => { export const updateItem = async (parent, { sub: subName, forward, options, ...item }, { me, models, lnd, hash, hmac }) => { // update iff this item belongs to me - const old = await models.item.findUnique({ where: { id: Number(item.id) } }) + const old = await models.item.findUnique({ where: { id: Number(item.id) }, include: { sub: true } }) if (Number(old.userId) !== Number(me?.id)) { throw new GraphQLError('item does not belong to you', { extensions: { code: 'FORBIDDEN' } }) } + if (old.subName !== subName) { + const sub = await models.sub.findUnique({ where: { name: subName } }) + if (old.freebie) { + if (!sub.allowFreebies) { + throw new GraphQLError(`~${subName} does not allow freebies`, { extensions: { code: 'BAD_INPUT' } }) + } + } else if (sub.baseCost > old.sub.baseCost) { + throw new GraphQLError('cannot change to a more expensive sub', { extensions: { code: 'BAD_INPUT' } }) + } + } // in case they lied about their existing boost await ssValidate(advSchema, { boost: item.boost }, { models, me, existingBoost: old.boost })