Skip to content

Commit

Permalink
unsubscribe user on territory unarchive or transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Jan 3, 2025
1 parent d53bc09 commit e5951fb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
16 changes: 15 additions & 1 deletion api/paidAction/territoryUnarchive.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,21 @@ export async function perform ({ name, invoiceId, ...data }, { me, cost, tx }) {
data.userId = me.id

if (sub.userId !== me.id) {
await tx.territoryTransfer.create({ data: { subName: name, oldUserId: sub.userId, newUserId: me.id } })
const oldUserId = sub.userId
const newUserId = me.id

await tx.territoryTransfer.create({ data: { subName: name, oldUserId, newUserId } })

// unsubscribe the old user
const oldSubscription = await tx.subSubscription.findUnique({ where: { userId_subName: { userId: oldUserId, subName: name } } })
if (oldSubscription) await tx.subSubscription.delete({ where: { userId_subName: { subName: name, userId: oldUserId } } })

// subscribe the new user if they aren't already
// await tx.subSubscription.upsert({
// where: { userId_subName: { userId: newUserId, subName: name } },
// update: {},
// create: { userId: newUserId, subName: name }
// })
}

await tx.subAct.create({
Expand Down
27 changes: 22 additions & 5 deletions api/resolvers/sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,31 @@ export default {
if (!user) {
throw new GqlInputError('user not found')
}
if (user.id === me.id) {

const oldUserId = me.id
const newUserId = user.id

if (newUserId === oldUserId) {
throw new GqlInputError('cannot transfer territory to yourself')
}

const [, updatedSub] = await models.$transaction([
models.territoryTransfer.create({ data: { subName, oldUserId: me.id, newUserId: user.id } }),
models.sub.update({ where: { name: subName }, data: { userId: user.id, billingAutoRenew: false } })
])
const updatedSub = await models.$transaction(async tx => {
await tx.territoryTransfer.create({ data: { subName, oldUserId, newUserId } })
const updatedSub = await tx.sub.update({ where: { name: subName }, data: { userId: newUserId, billingAutoRenew: false } })

// unsubscribe the old user
const oldSubscription = await tx.subSubscription.findUnique({ where: { userId_subName: { userId: oldUserId, subName } } })
if (oldSubscription) await tx.subSubscription.delete({ where: { userId_subName: { subName, userId: oldUserId } } })

// subscribe the new user if they aren't already
// await tx.subSubscription.upsert({
// where: { userId_subName: { userId: newUserId, subName } },
// update: {},
// create: { userId: newUserId, subName }
// })

return updatedSub
})

notifyTerritoryTransfer({ models, sub, to: user })

Expand Down

0 comments on commit e5951fb

Please sign in to comment.