Skip to content

Commit

Permalink
feat(SlackMessage): udpated slack message cleaner, concise with stron…
Browse files Browse the repository at this point in the history
…ger typescript
  • Loading branch information
crisner1978 committed Jun 4, 2024
1 parent 74afe70 commit c29eabd
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 280 deletions.
4 changes: 2 additions & 2 deletions src/app/api/credit-app/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { formatApplicant } from '@/utils/formatApplicant'
import { slackMsgRequest } from '@/utils/slackMsg'
import { NextResponse } from 'next/server'
import { withAppRouterHighlight } from '@/utils/withAppRouterHighlight'
import { slackMessageCredit } from '@/lib/actions/slack.message'

type RouteOnePayload = {
primaryBuyer: Record<string, any>
Expand Down Expand Up @@ -46,7 +46,7 @@ export const POST = withAppRouterHighlight(async (request: Request) => {
console.log('🚀 ~ CREDIT_API_DRIVLY ~ payload:', payload)

try {
await slackMsgRequest({ url: slackUrl, data })
await slackMessageCredit({ url: slackUrl, data })
const d = await fetch(`${process.env.CREDIT_API_DRIVLY}/applications`, {
method: 'POST',
body: JSON.stringify(payload),
Expand Down
84 changes: 84 additions & 0 deletions src/lib/actions/slack.helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import type { CreditApplicant } from 'typings'

export type SlackBlocks = Array<ReturnType<typeof createSlackSection> | { type: 'divider' }>

export function createSlackSection(text: string) {
return {
type: 'section',
text: {
type: 'mrkdwn',
text,
},
}
}

export function formatSlackApplicant(applicant: CreditApplicant, role: 'Primary' | 'Joint') {
const blocks: SlackBlocks = []
blocks.push({ type: 'divider' })
blocks.push(
createSlackSection(
`*${role} Applicant:*\n*Full name:* ${applicant.firstName} ${applicant.middleName || ''} ${
applicant.lastName
}\n*Phone:* ${applicant.phone} ${applicant.phoneType}\n*Email:* ${
applicant.email
}\n*Date of Birth:* ${applicant.dateOfBirth}\n*SSN:* ${applicant.ssn}`
)
)
blocks.push(
createSlackSection(
`*Residence*\n*Address:* ${applicant.addressLine1}\n*City:* ${applicant.city}\n*State:* ${
applicant.state
}\n*Zip Code:* ${applicant.zipCode}\n*Time at address:* ${applicant.addressYears} yrs ${
applicant.addressMonths ? applicant.addressMonths + 'mo' : ''
}\n*Monthly payment/rent:* ${applicant.rentMortgagePaymentAmount}`
)
)
if (Number(applicant.addressYears) < 2) {
blocks.push(
createSlackSection(
`*Previous Residence*\n*Address:* ${applicant.prevAddressLine1}\n*City:* ${
applicant.prevCity
}\n*State:* ${applicant.prevState}\n*Zip Code:* ${
applicant.prevZipCode
}\n*Time at address:* ${applicant.prevAddressYears} yrs ${
applicant.prevAddressMonths ? applicant.prevAddressMonths + 'mo' : ''
}\n*Monthly payment/rent:* ${applicant.prevRentMortgagePaymentAmount}`
)
)
}
blocks.push(
createSlackSection(
`*Drivers License*\n*License number:* ${applicant.licenseNumber}\n*State:* ${applicant.licenseState}`
)
)
blocks.push(
createSlackSection(
`*Employment History*\n*Employer name:* ${applicant.employerName}\n*Employment Status:* ${
applicant.employmentStatusCode
}\n*Employer phone:* ${applicant.employerPhone}\n*Occupation:* ${
applicant.employmentTitle
}\n*Time on job:* ${applicant.timeOnJobYears} yrs ${
applicant.timeOnJobMonths ? applicant.timeOnJobMonths + 'mo' : ''
}\n*Monthly income:* ${applicant.incomeAmount}`
)
)
if (Number(applicant.timeOnJobYears) < 2) {
blocks.push(
createSlackSection(
`*Previous Employment*\n*Employer name:* ${
applicant.previousEmployerName
}\n*Employer phone:* ${applicant.prevEmployerPhone}\n*Occupation:* ${
applicant.prevEmploymentTitle
}\n*Time on job:* ${applicant.prevTimeOnJobYears} yrs ${
applicant.prevTimeOnJobMonths ? applicant.prevTimeOnJobMonths + 'mo' : ''
}\n*Monthly income:* ${applicant.prevIncomeAmount}`
)
)
}
blocks.push(
createSlackSection(
`*Other Income*\n*Description:* ${applicant.otherIncomeSourceDescription}\n*Monthly Amount:* ${applicant.otherIncomeAmount}`
)
)
return blocks
}
57 changes: 57 additions & 0 deletions src/lib/actions/slack.message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use server'

import formatDate from '@/utils/formatDate'
import type { CreditApplicant, VehicleOfInterest } from 'typings'
import { createSlackSection, formatSlackApplicant, type SlackBlocks } from './slack.helpers'

interface SlackParams {
url: string | undefined
data: {
primary: CreditApplicant
secondary: CreditApplicant
vehicle: VehicleOfInterest
tradeIn: {
id: string
lienholder: string
allowance: number
}
}
}

export async function slackMessageCredit({ url, data }: SlackParams) {
const date = formatDate(new Date(), true)
const blocks: SlackBlocks = []

blocks.push(
createSlackSection(
`*${data.primary.firstName} ${data.primary.lastName}* | ${date} :moneybag:\n* ${data.primary.email} | ${date}*`
)
)
blocks.push(...formatSlackApplicant(data.primary, 'Primary'))

if (data.secondary) {
blocks.push(...formatSlackApplicant(data.secondary, 'Joint'))
}

blocks.push({ type: 'divider' })
blocks.push(
createSlackSection(
`*Vehicle of Interest*\n*Cash Down:* ${data?.vehicle?.cashDown}\n*VIN:* ${data.vehicle.vin}\n*Year:* ${data.vehicle.year}\n*Make:* ${data.vehicle.make}\n*Model:* ${data.vehicle.model}\n*Odometer:* ${data.vehicle.mileage}\n*Price:* ${data.vehicle.price}`
)
)

if (data.tradeIn) {
blocks.push({ type: 'divider' })
blocks.push(
createSlackSection(
`*Trade Information*\n*Record Id:* ${data.tradeIn.id}\n*Lender:* ${data.tradeIn.lienholder}\n*Trade In Allowance:* ${data.tradeIn.allowance}`
)
)
}

await fetch(url!, {
method: 'POST',
body: JSON.stringify({ blocks }),
headers: { 'Content-Type': 'application/json' },
})
}
231 changes: 0 additions & 231 deletions src/utils/slackMsg.ts

This file was deleted.

Loading

0 comments on commit c29eabd

Please sign in to comment.