270 lines
9.5 KiB
TypeScript
270 lines
9.5 KiB
TypeScript
/**
|
||
* ACOMP-01..12 — `admin.complaint` (catalogue §7.3).
|
||
*
|
||
* Complaints are created through the user API (`user.complaint.raise`) after the
|
||
* raiser has an address, because the complaint's `regionId` is resolved from the
|
||
* order → default address → user_regions chain.
|
||
*
|
||
* `getAll` / `resolve` are `protectedProcedure`, so a user token passes the
|
||
* guard and then dereferences `ctx.staffUser!` (the §13 gap pinned here).
|
||
*/
|
||
|
||
import { apiClient, expectTrpcError, uniqueSuffix, type ApiClient } from '../helpers/api'
|
||
import { anonClient, staffClient, superadminClient, userClient } from '../helpers/sessions'
|
||
import {
|
||
createAddress,
|
||
createRegion,
|
||
PASSWORD,
|
||
registerThrowawayUser,
|
||
type CreatedUser,
|
||
} from '../helpers/fixtures'
|
||
import { TEST_OTHER_REGION_ID, TEST_REGION_ID } from '../helpers/env'
|
||
import { getEssentialConsts } from '../helpers/cache'
|
||
|
||
jest.setTimeout(180_000)
|
||
|
||
async function createRegionStaff(regionId: number): Promise<ApiClient> {
|
||
const superadmin = await superadminClient()
|
||
const roles: any = await superadmin.superadmin.staff.roles.query()
|
||
const roleId = Number(roles.roles.find((r: any) => r.name === 'admin')?.id ?? roles.roles[0].id)
|
||
const name = `e2e_complaint_staff_${uniqueSuffix()}`
|
||
await superadmin.superadmin.staff.create.mutate({
|
||
name,
|
||
password: PASSWORD,
|
||
roleId,
|
||
regionId,
|
||
})
|
||
const login: any = await apiClient().admin.staffUser.login.mutate({ name, password: PASSWORD })
|
||
return apiClient(login.token)
|
||
}
|
||
|
||
/** A throwaway user whose default address pins the complaint to `regionId`. */
|
||
async function userInRegion(regionId: number = TEST_REGION_ID): Promise<CreatedUser> {
|
||
const user = await registerThrowawayUser()
|
||
await createAddress(user.token, regionId === TEST_REGION_ID ? {} : { regionId })
|
||
return user
|
||
}
|
||
|
||
async function raise(
|
||
user: CreatedUser,
|
||
complaintBody: string,
|
||
extra: { orderId?: string; imageUrls?: string[] } = {}
|
||
): Promise<any> {
|
||
return apiClient(user.token).user.complaint.raise.mutate({
|
||
complaintBody,
|
||
...extra,
|
||
})
|
||
}
|
||
|
||
async function getAll(admin: ApiClient, input: { cursor?: number; limit?: number } = {}) {
|
||
const res: any = await admin.admin.complaint.getAll.query({ limit: 100, ...input })
|
||
return res
|
||
}
|
||
|
||
describe('admin.complaint.getAll (ACOMP-01..05)', () => {
|
||
it('ACOMP-01: getAll lists the region’s complaints with user and status', async () => {
|
||
const admin = await staffClient()
|
||
const user = await userInRegion()
|
||
const body = `E2E complaint ${uniqueSuffix()}`
|
||
await raise(user, body)
|
||
|
||
const res = await getAll(admin)
|
||
const complaint = res.complaints.find((entry: any) => entry.text === body)
|
||
expect(complaint).toEqual(
|
||
expect.objectContaining({
|
||
id: expect.any(Number),
|
||
text: body,
|
||
userId: user.userId,
|
||
userName: user.name,
|
||
userMobile: user.mobile,
|
||
regionId: TEST_REGION_ID,
|
||
status: 'pending',
|
||
response: null,
|
||
})
|
||
)
|
||
expect(Array.isArray(complaint.images)).toBe(true)
|
||
})
|
||
|
||
it('ACOMP-02: limit and cursor paginate without overlap', async () => {
|
||
const admin = await staffClient()
|
||
const user = await userInRegion()
|
||
for (let i = 0; i < 3; i += 1) {
|
||
await raise(user, `E2E page complaint ${i} ${uniqueSuffix()}`)
|
||
}
|
||
|
||
const page1 = await getAll(admin, { limit: 2 })
|
||
expect(page1.complaints).toHaveLength(2)
|
||
expect(typeof page1.nextCursor).toBe('number')
|
||
|
||
const page2 = await getAll(admin, { limit: 2, cursor: page1.nextCursor })
|
||
const ids1 = page1.complaints.map((entry: any) => Number(entry.id))
|
||
const ids2 = page2.complaints.map((entry: any) => Number(entry.id))
|
||
|
||
expect(ids2.some((id: number) => ids1.includes(id))).toBe(false)
|
||
expect(ids2.every((id: number) => id < page1.nextCursor)).toBe(true)
|
||
expect(page2.complaints.length).toBeLessThanOrEqual(2)
|
||
})
|
||
|
||
it('ACOMP-03: a region with no complaints returns an empty list', async () => {
|
||
const region = await createRegion()
|
||
const regionAdmin = await createRegionStaff(region.regionId)
|
||
|
||
const res = await getAll(regionAdmin)
|
||
expect(res.complaints).toEqual([])
|
||
expect(res.nextCursor).toBeUndefined()
|
||
})
|
||
|
||
it('ACOMP-04: status maps to pending/resolved', async () => {
|
||
const admin = await staffClient()
|
||
const user = await userInRegion()
|
||
const pendingBody = `E2E pending ${uniqueSuffix()}`
|
||
const resolvedBody = `E2E resolved ${uniqueSuffix()}`
|
||
|
||
await raise(user, pendingBody)
|
||
await raise(user, resolvedBody)
|
||
|
||
const before = await getAll(admin)
|
||
const resolvedRow = before.complaints.find((entry: any) => entry.text === resolvedBody)
|
||
await admin.admin.complaint.resolve.mutate({
|
||
id: String(resolvedRow.id),
|
||
response: 'Handled',
|
||
})
|
||
|
||
const after = await getAll(admin)
|
||
expect(after.complaints.find((entry: any) => entry.text === pendingBody).status).toBe('pending')
|
||
expect(after.complaints.find((entry: any) => entry.text === resolvedBody).status).toBe('resolved')
|
||
})
|
||
|
||
it('ACOMP-05: complaint images are returned as scaffolded asset URLs', async () => {
|
||
const admin = await staffClient()
|
||
const user = await userInRegion()
|
||
const { assetsDomain } = await getEssentialConsts()
|
||
const image = `${assetsDomain}complaint-images/e2e-${uniqueSuffix()}.png`
|
||
const body = `E2E image complaint ${uniqueSuffix()}`
|
||
|
||
await raise(user, body, { imageUrls: [image] })
|
||
|
||
const res = await getAll(admin)
|
||
const complaint = res.complaints.find((entry: any) => entry.text === body)
|
||
expect(complaint.images).toEqual([image])
|
||
})
|
||
})
|
||
|
||
describe('admin.complaint.resolve (ACOMP-06..12)', () => {
|
||
it('ACOMP-06: resolve stores the response and flips the status', async () => {
|
||
const admin = await staffClient()
|
||
const user = await userInRegion()
|
||
const body = `E2E resolve ${uniqueSuffix()}`
|
||
await raise(user, body)
|
||
|
||
const before = await getAll(admin)
|
||
const row = before.complaints.find((entry: any) => entry.text === body)
|
||
expect(row.status).toBe('pending')
|
||
|
||
const res: any = await admin.admin.complaint.resolve.mutate({
|
||
id: String(row.id),
|
||
response: 'Your issue has been resolved',
|
||
})
|
||
expect(res).toEqual({ message: 'Complaint resolved successfully' })
|
||
|
||
const after = await getAll(admin)
|
||
const resolved = after.complaints.find((entry: any) => entry.text === body)
|
||
expect(resolved.status).toBe('resolved')
|
||
expect(resolved.response).toBe('Your issue has been resolved')
|
||
})
|
||
|
||
it('ACOMP-07 (current): an unknown complaint id is an INTERNAL_SERVER_ERROR', async () => {
|
||
// The resolver throws a plain `Error('Complaint not found')`, not an
|
||
// ApiError, so tRPC flattens it to 500 instead of 404.
|
||
const admin = await staffClient()
|
||
const error = await expectTrpcError(
|
||
() =>
|
||
admin.admin.complaint.resolve.mutate({
|
||
id: '999999999',
|
||
response: 'nobody is there',
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'Complaint not found'
|
||
)
|
||
expect(error).toBeInstanceOf(Error)
|
||
})
|
||
|
||
it('ACOMP-08: an empty response is BAD_REQUEST', async () => {
|
||
const admin = await staffClient()
|
||
const user = await userInRegion()
|
||
await raise(user, `E2E empty response ${uniqueSuffix()}`)
|
||
|
||
const res = await getAll(admin)
|
||
const row = res.complaints[0]
|
||
|
||
await expectTrpcError(
|
||
() => admin.admin.complaint.resolve.mutate({ id: String(row.id), response: '' }),
|
||
'BAD_REQUEST'
|
||
)
|
||
})
|
||
|
||
it('ACOMP-09: the stored response is trimmed', async () => {
|
||
const admin = await staffClient()
|
||
const user = await userInRegion()
|
||
const body = `E2E trimmed ${uniqueSuffix()}`
|
||
await raise(user, body)
|
||
|
||
const before = await getAll(admin)
|
||
const row = before.complaints.find((entry: any) => entry.text === body)
|
||
await admin.admin.complaint.resolve.mutate({
|
||
id: String(row.id),
|
||
response: ' padded response ',
|
||
})
|
||
|
||
const after = await getAll(admin)
|
||
expect(after.complaints.find((entry: any) => entry.text === body).response).toBe(
|
||
'padded response'
|
||
)
|
||
})
|
||
|
||
|
||
it('ACOMP-11: another region’s complaint is not found or listed', async () => {
|
||
const admin = await staffClient()
|
||
const otherUser = await userInRegion(TEST_OTHER_REGION_ID)
|
||
const body = `E2E other region ${uniqueSuffix()}`
|
||
await raise(otherUser, body)
|
||
|
||
const list = await getAll(admin)
|
||
expect(list.complaints.some((entry: any) => entry.text === body)).toBe(false)
|
||
|
||
// The region-2 admin can find it.
|
||
const otherAdmin = await createRegionStaff(TEST_OTHER_REGION_ID)
|
||
const otherList = await getAll(otherAdmin)
|
||
const otherRow = otherList.complaints.find((entry: any) => entry.text === body)
|
||
expect(otherRow).toBeTruthy()
|
||
expect(otherRow.regionId).toBe(TEST_OTHER_REGION_ID)
|
||
|
||
await expectTrpcError(
|
||
() =>
|
||
admin.admin.complaint.resolve.mutate({
|
||
id: String(otherRow.id),
|
||
response: 'Cross-region attempt',
|
||
}),
|
||
'INTERNAL_SERVER_ERROR',
|
||
'Complaint not found'
|
||
)
|
||
})
|
||
|
||
it('ACOMP-12 (current): a user token reaches the resolver and 500s on staffUser', async () => {
|
||
// Known §13 protectedProcedure gap: the guard accepts a user identity, then
|
||
// `ctx.staffUser!.regionId` throws. Pinned, not intended.
|
||
const user = await userClient()
|
||
await expectTrpcError(
|
||
() => user.admin.complaint.getAll.query({ limit: 1 }),
|
||
'INTERNAL_SERVER_ERROR'
|
||
)
|
||
await expectTrpcError(
|
||
() => user.admin.complaint.resolve.mutate({ id: '1', response: 'nope' }),
|
||
'INTERNAL_SERVER_ERROR'
|
||
)
|
||
await expectTrpcError(
|
||
() => anonClient().admin.complaint.getAll.query({ limit: 1 }),
|
||
'UNAUTHORIZED'
|
||
)
|
||
})
|
||
})
|