Skip to content

Commit

Permalink
Lint et optimisations
Browse files Browse the repository at this point in the history
  • Loading branch information
gfra54 committed Apr 20, 2024
1 parent a471834 commit 42b87e9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 52 deletions.
51 changes: 3 additions & 48 deletions lib/migration/migration-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import * as Ticket from '../models/ticket.js'
import * as Subscription from '../models/subscription.js'
import * as Membership from '../models/membership.js'
import {formatPrice} from '../models/purchase.js'
import {isAfter} from 'date-fns'
import process from 'node:process'
import util from 'node:util'

/**
* Calculates and returns the total quantity of each type of product in the provided array.
Expand Down Expand Up @@ -82,7 +79,7 @@ export function createPurchaseObjectTicket(product) {
orderReference: null,
productType,
legacy: true,
price: formatPrice(product.tickets * ticketUnitPriceByDate(product.purchaseDate))
price: formatPrice(product.tickets * Ticket.ticketUnitPriceByDate(product.purchaseDate))
}
}

Expand All @@ -100,7 +97,7 @@ export function createPurchaseObjectMembership(product) {
productType: 'membership',
membershipStart: product.membershipStart,
legacy: true,
price: formatPrice(membershipUnitPriceByDate(product.purchaseDate))
price: formatPrice(Membership.membershipUnitPriceByDate(product.purchaseDate))
}
}

Expand All @@ -118,52 +115,10 @@ export function createPurchaseObjectAbo(product) {
productType: 'subscription',
startDate: product.aboStart,
legacy: true,
price: formatPrice(subscriptionUnitPriceByDate(product.purchaseDate))
price: formatPrice(Subscription.subscriptionUnitPriceByDate(product.purchaseDate))
}
}

/**
* Calculate the unit price of a subscription based on its date.
*
* @param {Date} date - The date the subscription was purchased.
* @returns {number} The unit price of the subscription. If the date is after February 1, 2017, the price is 60; otherwise it's 0.
*/
export function subscriptionUnitPriceByDate(date) {
if (isAfter(new Date(date), new Date('2017-02-01'))) {
return 60
}

return 0
}

/**
* Calculate the unit price of a ticket based on its date.
*
* @param {Date} date - The date the ticket was purchased.
* @returns {number} The unit price of the ticket. If the date is after February 1, 2017, the price is 60; otherwise it's 0.
*/
export function ticketUnitPriceByDate(date) {
if (isAfter(new Date(date), new Date('2017-02-01'))) {
return 60
}

return 0
}

/**
* Calculate the unit price of a membership based on its date.
*
* @param {Date} date - The date the membership was purchased.
* @returns {number} The unit price of the membership. If the date is after January 1, 2014, the price is 10; otherwise it's 0.
*/
export function membershipUnitPriceByDate(date) {
if (isAfter(new Date(date), new Date('2014-01-01'))) {
return 10
}

return 0
}

/**
* Adds order information to a set of purchases.
*
Expand Down
15 changes: 15 additions & 0 deletions lib/models/membership.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import mongo from '../util/mongo.js'
import {customAlphabet} from 'nanoid'
import {isAfter} from 'date-fns'

const nanoid = customAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')

Expand Down Expand Up @@ -47,3 +48,17 @@ export async function updateMembership(_id, set) {
}
)
}

/**
* Calculate the unit price of a membership based on its date.
*
* @param {Date} date - The date the membership was purchased.
* @returns {number} The unit price of the membership. If the date is after January 1, 2014, the price is 10; otherwise it's 0.
*/
export function membershipUnitPriceByDate(date) {
if (isAfter(new Date(date), new Date('2014-01-01'))) {
return 10
}

return 0
}
15 changes: 15 additions & 0 deletions lib/models/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ export async function updateSubscription(_id, set) {
}
)
}

/**
* Calculate the unit price of a subscription based on its date.
*
* @param {Date} date - The date the subscription was purchased.
* @returns {number} The unit price of the subscription. If the date is after February 1, 2017, the price is 60; otherwise it's 0.
*/
export function subscriptionUnitPriceByDate(date) {
if (isAfter(new Date(date), new Date('2017-02-01'))) {
return 60
}

return 0
}

20 changes: 16 additions & 4 deletions lib/models/ticket.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {customAlphabet} from 'nanoid'

const nanoid = customAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz')

// Const TICKET_UNIT_COST_IN_EUR = 6 // As of 2017-02-01
const TICKET_UNIT_COST_IN_EUR = 6 // As of 2017-02-01

export async function getMemberTickets(memberId) {
const user = await mongo.db.collection('users')
Expand All @@ -17,9 +17,7 @@ export async function getMemberTickets(memberId) {
_id: ticket.purchaseDate, // Until there is a unique id
count: ticket.tickets,
purchased: ticket.purchaseDate,
amount: isAfter(new Date(ticket.purchaseDate), new Date('2017-02-01'))
? TICKET_UNIT_COST_IN_EUR * ticket.tickets
: 0
amount: ticketUnitPriceByDate(ticket.purchaseDate) * ticket.tickets
}))
.orderBy(['purchased'], ['desc'])
.value()
Expand Down Expand Up @@ -96,3 +94,17 @@ export function computeTicketsQuantity(tickets, productType) {

return total
}

/**
* Calculate the unit price of a ticket based on its date.
*
* @param {Date} date - The date the ticket was purchased.
* @returns {number} The unit price of the ticket. If the date is after February 1, 2017, the price is 60; otherwise it's 0.
*/
export function ticketUnitPriceByDate(date) {
if (isAfter(new Date(date), new Date('2017-02-01'))) {
return TICKET_UNIT_COST_IN_EUR
}

return 0
}

0 comments on commit 42b87e9

Please sign in to comment.