This repository was archived by the owner on Apr 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Fixes before stitches #4
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
servers/republik/modules/crowdfundings/lib/activateYearlyMembership.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const moment = require('moment') | ||
const { ascending } = require('d3-array') | ||
|
||
module.exports = async (memberships, pgdb, dryRun) => { | ||
const membershipTypeIndex = ( | ||
await pgdb.public.membershipTypes.findAll() | ||
).reduce( | ||
(index, type) => { | ||
index[type.id] = type | ||
return index | ||
}, | ||
{} | ||
) | ||
const packagesIndex = ( | ||
await pgdb.public.packages.findAll() | ||
).reduce( | ||
(index, p) => { | ||
index[p.id] = p | ||
return index | ||
}, | ||
{} | ||
) | ||
|
||
const yearlyMemberships = memberships.filter(m => membershipTypeIndex[m.membershipTypeId].name !== 'MONTHLY_ABO') | ||
if (!yearlyMemberships.length) { | ||
return | ||
} | ||
|
||
const membershipPeriods = await pgdb.public.membershipPeriods.find({ | ||
membershipId: memberships.map(m => m.id) | ||
}) | ||
const unusedMemberships = yearlyMemberships.filter(m => !membershipPeriods.find(p => p.membershipId === m.id)) | ||
if (!unusedMemberships.length) { | ||
return | ||
} | ||
|
||
const pledges = await pgdb.public.pledges.find({ | ||
id: memberships.map(m => m.pledgeId) | ||
}) | ||
|
||
const inactiveMemberships = unusedMemberships.filter(m => { | ||
const pledge = pledges.find(p => p.id === m.pledgeId) | ||
return packagesIndex[pledge.packageId].name !== 'ABO_GIVE' || !m.voucherCode | ||
}) | ||
if (!inactiveMemberships.length) { | ||
return | ||
} | ||
|
||
inactiveMemberships.sort((a, b) => ascending(a.sequenceNumber, b.sequenceNumber)) | ||
|
||
const electedMembership = ( | ||
inactiveMemberships.find(m => m.voucherCode === null) || | ||
inactiveMemberships.find(m => m.reducedPrice) || | ||
inactiveMemberships.find(m => membershipTypeIndex[m.membershipTypeId].name === 'BENEFACTOR_ABO') || | ||
inactiveMemberships[0] | ||
) | ||
|
||
if (dryRun) { | ||
return electedMembership | ||
} | ||
|
||
const beginDate = new Date() // now | ||
const endDate = moment(beginDate).add(1, 'year') | ||
await pgdb.public.memberships.update({ | ||
id: electedMembership.id | ||
}, { | ||
active: true, | ||
renew: true, | ||
voucherCode: null, | ||
voucherable: false | ||
}) | ||
|
||
await pgdb.public.membershipPeriods.insert({ | ||
membershipId: electedMembership.id, | ||
beginDate, | ||
endDate | ||
}) | ||
|
||
return electedMembership | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this line necessary considering
|| !m.voucherCode
in line 43?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes because above it ensures that only none give abos or redeemed give abos are considere for activation. Here it says that a reedemed abo should be used first (same logic as January activation).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, so basically a
null
voucherCode represents redeemed, while a "normal" membership would have anundefined
voucherCode, correct? (Just trying to understand)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normal inactive memberships have a voucherCode string