Skip to content

Commit

Permalink
Identifier les données migrées par migratedFromUsersCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
gfra54 committed Apr 24, 2024
1 parent aae347b commit 936dcaa
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 23 deletions.
13 changes: 7 additions & 6 deletions lib/migration/migration-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function processPurchasesProductType(product, productType) {
*
* @param {Object} product - The product details.
*
* @returns {Object} Returns a purchase object tickets properties, 'legacy' and 'price'.
* @returns {Object} Returns a purchase object tickets properties, 'migratedFromUsersCollection' and 'price'.
*/
export function createPurchaseObjectTicket(product) {
let productType = 'ticketsBook'
Expand All @@ -78,7 +78,7 @@ export function createPurchaseObjectTicket(product) {
purchaseDate: product.purchaseDate,
orderReference: null,
productType,
legacy: true,
migratedFromUsersCollection: true,
price: formatPrice(product.tickets * Ticket.ticketUnitPriceByDate(product.purchaseDate))
}
}
Expand All @@ -88,15 +88,15 @@ export function createPurchaseObjectTicket(product) {
*
* @param {Object} product - The product object from which the purchase is being made.
*
* @returns {Object} Returns a purchase object memberships properties, 'legacy' and 'price'.
* @returns {Object} Returns a purchase object memberships properties, 'migratedFromUsersCollection' and 'price'.
*/
export function createPurchaseObjectMembership(product) {
return {
purchaseDate: product.purchaseDate,
orderReference: null,
productType: 'membership',
membershipStart: product.membershipStart,
legacy: true,
migratedFromUsersCollection: true,
price: formatPrice(Membership.membershipUnitPriceByDate(product.purchaseDate))
}
}
Expand All @@ -106,15 +106,15 @@ export function createPurchaseObjectMembership(product) {
*
* @param {Object} product - The product details.
*
* @returns {Object} Returns a purchase object subscriptions properties, 'legacy' and 'price'.
* @returns {Object} Returns a purchase object subscriptions properties, 'migratedFromUsersCollection' and 'price'.
*/
export function createPurchaseObjectAbo(product) {
return {
purchaseDate: product.purchaseDate,
orderReference: null,
productType: 'subscription',
startDate: product.aboStart,
legacy: true,
migratedFromUsersCollection: true,
price: formatPrice(Subscription.subscriptionUnitPriceByDate(product.purchaseDate))
}
}
Expand Down Expand Up @@ -179,3 +179,4 @@ export function legacyCollectionName(productType) {

return collectionName
}

31 changes: 25 additions & 6 deletions lib/migration/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export async function importOrder(order) {
}

const userId = user._id
const purchases = await Purchase.findByMemberAndDate(userId, purchaseDate, {orderReference: null, legacy: true})
const purchases = await Purchase.findByMemberAndDate(userId, purchaseDate, {orderReference: null, migratedFromUsersCollection: true})
const payload = {}
for (const product of products) {
const {productType} = product
Expand Down Expand Up @@ -189,7 +189,7 @@ export function handleOrderProduct(product, purchases, options = {}) {

const {mustBeCoherent, order} = options
const quantities = MigrationTools.computeProductQuantities(order.products)
const {subscriptions, memberships, tickets} = purchases

/**
* Adds the order informations in the local collections. If asked, will assess whether the quantity is coherent or
* not between the wordpress order and the local collection.
Expand All @@ -211,6 +211,16 @@ export function handleOrderProduct(product, purchases, options = {}) {
}
}

/**
* Checks if the quantity of items matches certain defined conditions.
*
* @param {Array} items - The items to check.
* @param {string} type - The type of item to check for.
* @param {function} [quantityCompute=null] - Optional function to compute the quantity of items. If not provided, the length of the items array will be taken.
* @param {number} [multiplier=1] - Multiplier to scale the product's quantity.
*
* @returns {boolean} Returns true if the quantity of items matches any of the defined conditions (product.quantity * multiplier, quantities[type], or items.length === quantities[type]), false otherwise.
*/
const isCoherent = (items, type, quantityCompute = null, multiplier = 1) => {
const itemsQuantity = quantityCompute ? quantityCompute(items, type) : items.length

Expand All @@ -229,8 +239,17 @@ export function handleOrderProduct(product, purchases, options = {}) {
return false
}

handleOrderProductByType(tickets, 'ticketsBook', Ticket.computeTicketsQuantity, 10)
handleOrderProductByType(tickets, 'singleTicket', Ticket.computeTicketsQuantity)
handleOrderProductByType(memberships, 'membership')
handleOrderProductByType(subscriptions, 'subscription')
const collectionName = MigrationTools.convertProductTypeToCollectionName(productType)
let quantityCompute = null
let multiplier = 1

if (collectionName === 'tickets') {
quantityCompute = Ticket.computeTicketsQuantity
}

if (productType === 'ticketsBook') {
multiplier = 10
}

return handleOrderProductByType(purchases[collectionName], productType, quantityCompute, multiplier)
}
2 changes: 1 addition & 1 deletion lib/models/membership.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function addMembershipToMember(memberId, membershipStart, purchase,
payloads.push({...payload})
}

if (!purchase.migratedFromUsers) {
if (!purchase.migratedFromUsersCollection) {
await addMembershipToMemberLegacy(memberId, purchase.purchaseDate, membershipStart) // For data consistency
}

Expand Down
15 changes: 7 additions & 8 deletions lib/models/purchase.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function updatePurchases(purchases) {
* @param {string} orderReference - A unique reference for the order.
* @param {Date} purchaseDate - The date of the purchase.
* @param {Object} purchase - An object containing information about the purchase.
* @param {number} purchase.price - The price of the product purchased.
* @param {number} purchase.price - The price of the product purchased
* @param {string} purchase.productType - The type of product that was purchased.
* @returns {Object} Returns an object with properties purchaseDate, orderReference, productType, and formatted price.
*/
Expand All @@ -77,13 +77,12 @@ export function formatPurchase(orderReference, purchaseDate, purchase) {
}

/**
* Converts the price imported from the ecommerce website from a string to a Decimal128 version of the price, in cents
* Converts the price imported from the ecommerce website from a string
* @param {string} priceString
* @returns {Decimal128}
* @returns {Float}
*/
export function formatPrice(priceString) {
let price = Decimal128.fromString(String(priceString))
price *= 100 // Common practice is to store the value in the smallest denomination (e.g., cents for euros)
const price = Number.parseFloat(String(priceString))
return price
}

Expand All @@ -107,7 +106,7 @@ export function getYears() {
* @returns null
*/
export async function deleteImportedPurchases() {
await mongo.db.collection('tickets').deleteMany({legacy: true})
await mongo.db.collection('subscriptions').deleteMany({legacy: true})
await mongo.db.collection('memberships').deleteMany({legacy: true})
await mongo.db.collection('tickets').deleteMany({migratedFromUsersCollection: true})
await mongo.db.collection('subscriptions').deleteMany({migratedFromUsersCollection: true})
await mongo.db.collection('memberships').deleteMany({migratedFromUsersCollection: true})
}
2 changes: 1 addition & 1 deletion lib/models/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function addSubscriptionsToMember(memberId, startDate, purchase, pu
payloads.push({...payload})
}

if (!purchase.migratedFromUsers) {
if (!purchase.migratedFromUsersCollection) {
await addSubscriptionsToMemberLegacy(memberId, purchase.purchaseDate, startDate, purchaseQuantity) // For data consistency
}

Expand Down
2 changes: 1 addition & 1 deletion lib/models/ticket.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export async function addTicketsToMember(memberId, ticketsQuantity, purchase, pu
payloads.push({...payload})
}

if (!purchase.migratedFromUsers) {
if (!purchase.migratedFromUsersCollection) {
await addTicketsToMemberLegacy(memberId, purchase.purchaseDate, purchaseQuantity * ticketsQuantity)
}

Expand Down

0 comments on commit 936dcaa

Please sign in to comment.