Skip to content
This repository was archived by the owner on Apr 6, 2022. It is now read-only.

Fix Postfinance import #535

Merged
merged 2 commits into from
Nov 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ const DRY_RUN = process.env.DRY_RUN_SEND_PAYMENT_REMINDERS === 'true'

const FIRST_REMINDER_DEADLINE_DAYS = PAYMENT_DEADLINE_DAYS + 3 // 3 days to consider weekends
const SECOND_REMINDER_DEADLINE_DAYS = FIRST_REMINDER_DEADLINE_DAYS + 30
const CANCEL_PLEDGE_DEADLINE_DAYS = SECOND_REMINDER_DEADLINE_DAYS + 15
const DAYS_TO_CONSIDER = CANCEL_PLEDGE_DEADLINE_DAYS + 30
const DAYS_TO_CONSIDER = SECOND_REMINDER_DEADLINE_DAYS + 30

interface OutstandingPayment {
paymentId: Nominal<string, 'paymentId'>
Expand Down Expand Up @@ -46,19 +45,16 @@ export async function sendPaymentReminders(

const [
firstRemindersSent,
secondRemindersSent,
cancelMessages,
secondRemindersSent
] = await Promise.all([
sendFirstReminder(outstandingPayments, context, dryRun),
sendSecondReminder(outstandingPayments, context, dryRun),
getMessageForAccountsToCancel(outstandingPayments),
sendSecondReminder(outstandingPayments, context, dryRun)
])

const message = generateReport({
dryRun,
firstRemindersSent,
secondRemindersSent,
cancelMessages,
secondRemindersSent
})

await publishFinance(message)
Expand All @@ -68,42 +64,19 @@ export async function sendPaymentReminders(
function generateReport({
dryRun,
firstRemindersSent,
secondRemindersSent,
cancelMessages,
secondRemindersSent
}: {
dryRun: boolean
firstRemindersSent: number
secondRemindersSent: number
cancelMessages: string[]
}) {
let messageLines = [
return [
...(dryRun ? ['⚠️ DRY RUN, REMINDERS ARE NOT SENT ⚠️'] : []),
'💌 Reminders Sent',
'',
`First reminders sent: ${firstRemindersSent}`,
`SecondRemindersSent: ${secondRemindersSent}`,
]

if (cancelMessages.length) {
messageLines = [
...messageLines,
'',
'🚨 Action Required - Cancel Pledges 🚨',
...cancelMessages,
]
}

const message = messageLines.join('\n')
return message
}

function getMessageForAccountsToCancel(
outstandingPayments: OutstandingPayment[],
) {
const filterDate = daysAgo(CANCEL_PLEDGE_DEADLINE_DAYS)
return outstandingPayments
.filter(({ createdAt }) => createdAt < filterDate)
.map(paymentToMessage)
].join('\n')
}

function paymentToMessage({ userId, pledgeId }: OutstandingPayment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ const insertPayments = async ({
return await postfinancePaymentsTable.insert(record)
})

return (await Promise.all(insertPromises)).reduce((a, b) => a + b)
return (await Promise.all(insertPromises)).reduce((a, b) => a + b, 0)
}

interface PostfinancePaymentRecord {
Expand Down