Skip to content

Commit

Permalink
fix(subscription): also update matching abo in user.profile
Browse files Browse the repository at this point in the history
  • Loading branch information
mtthp committed Dec 26, 2024
1 parent 801f742 commit 2c6634b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ export async function updateMemberSubscription(req, res) {
startDate: startDate.toISOString().slice(0, 10)
})

// TODO: remove this legacy update once "abos" has been removed from the users collection
await Subscription.updateAboStartDateInUserLegacy(
memberId,
subscription.started,
updatedSubscription.startDate
)

Audit.logAuditTrail(req.user, 'MEMBER_SUBSCRIPTION_UPDATE', {
memberId,
subscriptionId,
Expand Down
1 change: 1 addition & 0 deletions lib/models/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export async function getMemberActivity(memberId) {
throw createHttpError(404, 'Member not found')
}

// TODO: we should retrieve subscriptions from the "subscriptions" collection instead of the user profile
const {abos} = user.profile

const activity = await mongo.db.collection('member_activity')
Expand Down
25 changes: 25 additions & 0 deletions lib/models/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,34 @@ export async function updateSubscription(_id, set) {
},
{returnDocument: 'after'}
)

return updatedSubscription.value
}

// TODO: deprecated, to remove once "abos" has been removed from the users collection
export async function updateAboStartDateInUserLegacy(memberId, previousStartDate, newStartDate) {
const user = await mongo.db.collection('users').findOne({_id: memberId})
const updatedAbos = user.profile.abos.map(abo => {
if (abo.aboStart === previousStartDate) {
return {
...abo,
aboStart: newStartDate
}
}

return abo
})

await mongo.db.collection('users').updateOne(
{_id: memberId},
{
$set: {
'profile.abos': updatedAbos
}
}
)
}

/**
* Calculate the unit price of a subscription based on its date.
*
Expand Down

0 comments on commit 2c6634b

Please sign in to comment.