Skip to content
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
51 changes: 35 additions & 16 deletions backend/src/api/contacts/contacts.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1174,8 +1174,8 @@ describe('ContactsService', () => {
})
})

describe('cancelReasonCode on SET_NOT_ELIGIBLE', () => {
it('should default cancelReasonCode to 21 when IN_PAY and not already set', async () => {
describe('cancellation fields on SET_NOT_ELIGIBLE', () => {
it('should set cancelReasonCode and careEndDate from in_pay', async () => {
const contact = { id: 1, csaStatus: 'in_pay', cancelReasonCode: null, resumeStatus: null }
vi.spyOn(prisma.contact, 'findUnique').mockResolvedValue(contact as any)
const updateSpy = vi.spyOn(prisma.contact, 'update').mockResolvedValue({} as any)
Expand All @@ -1186,17 +1186,18 @@ describe('ContactsService', () => {

expect(result.success).toBe(true)
expect(result.to).toBe('not_eligible_ip_tbd')
expect(updateSpy).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
cancelReasonCode: '21',
}),
}),
)
const updateCall = updateSpy.mock.calls[0][0] as any
expect(updateCall.data.cancelReasonCode).toBe('21')
expect(updateCall.data.careEndDate).toBeInstanceOf(Date)
})

it('should NOT overwrite cancelReasonCode when already set', async () => {
const contact = { id: 1, csaStatus: 'in_pay', cancelReasonCode: '14', resumeStatus: null }
it('should set cancelReasonCode and careEndDate from eligible_tbd', async () => {
const contact = {
id: 1,
csaStatus: 'eligible_tbd',
cancelReasonCode: null,
resumeStatus: null,
}
vi.spyOn(prisma.contact, 'findUnique').mockResolvedValue(contact as any)
const updateSpy = vi.spyOn(prisma.contact, 'update').mockResolvedValue({} as any)

Expand All @@ -1205,17 +1206,18 @@ describe('ContactsService', () => {
})

expect(result.success).toBe(true)
expect(result.to).toBe('not_eligible_ip_tbd')
expect(result.to).toBe('not_eligible_out_of_pay')
const updateCall = updateSpy.mock.calls[0][0] as any
expect(updateCall.data).not.toHaveProperty('cancelReasonCode')
expect(updateCall.data.cancelReasonCode).toBe('21')
expect(updateCall.data.careEndDate).toBeInstanceOf(Date)
})

it('should NOT set cancelReasonCode for SET_NOT_ELIGIBLE from non-IN_PAY state', async () => {
it('should set cancelReasonCode and careEndDate from on_hold', async () => {
const contact = {
id: 1,
csaStatus: 'eligible_tbd',
csaStatus: 'on_hold',
cancelReasonCode: null,
resumeStatus: null,
resumeStatus: 'eligible_tbd',
}
vi.spyOn(prisma.contact, 'findUnique').mockResolvedValue(contact as any)
const updateSpy = vi.spyOn(prisma.contact, 'update').mockResolvedValue({} as any)
Expand All @@ -1227,7 +1229,24 @@ describe('ContactsService', () => {
expect(result.success).toBe(true)
expect(result.to).toBe('not_eligible_out_of_pay')
const updateCall = updateSpy.mock.calls[0][0] as any
expect(updateCall.data.cancelReasonCode).toBe('21')
expect(updateCall.data.careEndDate).toBeInstanceOf(Date)
})

it('should NOT overwrite cancelReasonCode when already set but still set careEndDate', async () => {
const contact = { id: 1, csaStatus: 'in_pay', cancelReasonCode: '14', resumeStatus: null }
vi.spyOn(prisma.contact, 'findUnique').mockResolvedValue(contact as any)
const updateSpy = vi.spyOn(prisma.contact, 'update').mockResolvedValue({} as any)

const result = await service.updateCsaStatus(1, 'SET_NOT_ELIGIBLE', 'USER', {
userId: 'user1',
})

expect(result.success).toBe(true)
expect(result.to).toBe('not_eligible_ip_tbd')
const updateCall = updateSpy.mock.calls[0][0] as any
expect(updateCall.data).not.toHaveProperty('cancelReasonCode')
expect(updateCall.data.careEndDate).toBeInstanceOf(Date)
})
})

Expand Down
15 changes: 7 additions & 8 deletions backend/src/api/contacts/contacts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'src/common/state-machine/constants'
import type { Actor, TransitionResult } from 'src/common/state-machine/interfaces'
import { StateMachineService } from 'src/common/state-machine/state-machine.service'
import { enrichLabels, isEligibleAge } from 'src/common/utils'
import { enrichLabels, isEligibleAge, pacificToday } from 'src/common/utils'
import { IcmSyncBackService } from 'src/sync/icm/icm-sync-back.service'
import { ALLOWED_FILTER_SORT_FIELDS, BULK_OPERATION_SKIP_REASONS } from './constants'
import { ContactDto } from './dto/contact.dto'
Expand Down Expand Up @@ -300,13 +300,12 @@ export class ContactsService {
updateData.preBatchStatus = null
}

// default cancel reason code when user sets not eligible from in-pay
if (
event === CSA_EVENT.SET_NOT_ELIGIBLE &&
currentState === CSA_STATUS.IN_PAY &&
!contact.cancelReasonCode
) {
updateData.cancelReasonCode = '21'
// set cancellation fields when user sets not eligible
if (event === CSA_EVENT.SET_NOT_ELIGIBLE) {
if (!contact.cancelReasonCode) {
updateData.cancelReasonCode = '21'
}
updateData.careEndDate = pacificToday()
}

await this.prisma.contact.update({
Expand Down
Loading