diff --git a/lib/api.js b/lib/api.js index 802034e..d607614 100644 --- a/lib/api.js +++ b/lib/api.js @@ -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, diff --git a/lib/models/activity.js b/lib/models/activity.js index 2f2bc18..268d059 100644 --- a/lib/models/activity.js +++ b/lib/models/activity.js @@ -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') diff --git a/lib/models/subscription.js b/lib/models/subscription.js index 5809efe..8712a16 100644 --- a/lib/models/subscription.js +++ b/lib/models/subscription.js @@ -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. *