Skip to content

Commit

Permalink
Find conditions in addition to coupons
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Sep 19, 2024
1 parent c94f942 commit 076403f
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions packages/payloadset/packages/payments/src/Discount/Diviner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
Condition,
Coupon,
Discount,
EscrowTerms, isCoupon,
EscrowTerms, isConditionWithMeta, isCoupon,
isCouponWithMeta,
isEscrowTerms, NO_DISCOUNT, PaymentDiscountDivinerConfigSchema, PaymentDiscountDivinerParams,
} from '@xyo-network/payment-payload-plugins'
Expand Down Expand Up @@ -145,35 +145,56 @@ export class PaymentDiscountDiviner<
* Finds the discounts specified by the escrow terms from the supplied payloads
* @param terms The escrow terms
* @param hashMap The payloads to search for the discounts
* @returns The discounts found in the payloads
* @returns A tuple containing all the escrow coupons and conditions referenced in those coupons
* that were found in the either the supplied payloads or the archivist
*/
protected async getEscrowDiscounts(terms: EscrowTerms, hashMap: Record<Hash, Payload>): Promise<[Coupon[], Condition[]]> {
// Parse discounts
const hashes = terms.discounts ?? []
if (hashes.length === 0) return [[], []]
const discountsHashes = terms.discounts ?? []
if (discountsHashes.length === 0) return [[], []]

// Use the supplied payloads to find the discounts
const discounts: Coupon[] = hashes.map(hash => hashMap[hash]).filter(exists).filter(isCoupon)
const missing = hashes.filter(hash => !hashMap[hash])
const discounts: Coupon[] = discountsHashes.map(hash => hashMap[hash]).filter(exists).filter(isCoupon)
const missingDiscounts = discountsHashes.filter(hash => !hashMap[hash])
// If not all discounts are found
if (missing.length > 0) {
if (missingDiscounts.length > 0) {
// Find any remaining from discounts archivist
const discountsArchivist = await this.getDiscountsArchivist()
const payloads = await discountsArchivist.get(missing)
const payloads = await discountsArchivist.get(missingDiscounts)
discounts.push(...payloads.filter(isCouponWithMeta))
}
// If not all discounts are found
if (discounts.length !== hashes.length) {
if (discounts.length !== discountsHashes.length) {
const termsHash = await PayloadBuilder.hash(terms)
const foundHashes = await PayloadBuilder.hashes(discounts)
// Log individual discounts that were not found
for (const hash of hashes) {
for (const hash of discountsHashes) {
if (!foundHashes.includes(hash)) console.warn(`Discount ${hash} not found for terms ${termsHash}`)
}
}

// TODO: Find all non-supplied coupon conditions here as well
return [discounts, []]
const conditionsHashes: Hash[] = discounts.flatMap(discount => discount.conditions ?? [])
const conditions: Condition[] = conditionsHashes.map(hash => hashMap[hash]).filter(exists).filter(isConditionWithMeta)
const missingConditions = conditionsHashes.filter(hash => !hashMap[hash])

// If not all conditions are found
if (missingConditions.length > 0) {
// Find any remaining from discounts archivist
const discountsArchivist = await this.getDiscountsArchivist()
const payloads = await discountsArchivist.get(missingConditions)
conditions.push(...payloads.filter(isConditionWithMeta))
}
// If not all conditions are found
if (conditions.length !== conditionsHashes.length) {
const termsHash = await PayloadBuilder.hash(terms)
const foundHashes = await PayloadBuilder.hashes(conditions)
// Log individual conditions that were not found
for (const hash of discountsHashes) {
if (!foundHashes.includes(hash)) console.warn(`Coupon condition ${hash} not found for terms ${termsHash}`)
}
}

return [discounts, conditions]
}

protected isCouponCurrent(coupon: Coupon): boolean {
Expand Down

0 comments on commit 076403f

Please sign in to comment.