Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsubscribe original territory owner on transfer or unarchive #1791

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion api/paidAction/territoryUnarchive.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ 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 } } })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you prefer this or deleteMany?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think delete should be fine. But do you need to check first if it exists?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, delete will error if the record does not exist (eg. if the user unsubscribed manually before transferring the territory) the workaround is to use deleteMany that will not error.
But in this case, since it is already inside a transaction, maybe this is better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use deleteMany then

}

await tx.subAct.create({
Expand Down
19 changes: 14 additions & 5 deletions api/resolvers/sub.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,23 @@ 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 } } })
return updatedSub
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it also subscribe the new user if they aren't already?
On one hand, I think it would be expected to do so, but on the other hand, you can transfer territories without the receiver's consent, so maybe it shouldn’t subscribe automatically?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, I'd say let's not automatically subscribe them for the reason you mentioned

})

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

Expand Down
Loading