fix: map GL response integers to string enums for Cheque Management fields
This commit is contained in:
@@ -37,6 +37,11 @@ import {
|
||||
ReceivedFromType,
|
||||
CreateReceivedChequeRequest,
|
||||
UpdateReceivedChequeStatusRequest,
|
||||
CHEQUE_BOOK_STATUS_BY_CODE,
|
||||
CHEQUE_PAGE_ISSUE_STATUS_BY_CODE,
|
||||
PAYEE_TYPE_BY_CODE,
|
||||
RECEIVED_FROM_TYPE_BY_CODE,
|
||||
RECEIVED_CHEQUE_STATUS_BY_CODE,
|
||||
} from "@/types/general-ledger"
|
||||
|
||||
const GL_BASE = "/api/v1/gl"
|
||||
@@ -257,50 +262,100 @@ export const cashAccountTypesApi = {
|
||||
* routes, not a numeric id. No `list()`/`get()` for pages standalone — a book's pages are always
|
||||
* read via `get(chequeBookNo, true)`'s `pages[]`, which is the only place this frontend needs them.
|
||||
*/
|
||||
// GL sends `ChequeBook.status`/`ChequePage.issueStatus`/`ChequePage.payeeType`/
|
||||
// `ReceivedCheque.receivedFromType`/`ReceivedCheque.status` as raw integers, not their string
|
||||
// name (06_Enums_Reference.md — no global JsonStringEnumConverter on GL's side; see the long
|
||||
// comment above the `*_BY_CODE` maps in types/general-ledger.ts for why). These `Raw*` shapes
|
||||
// describe exactly what GL's JSON actually contains for those fields; the `map*` functions below
|
||||
// translate them into this frontend's normal string-enum `ChequeBook`/`ChequePage`/`ReceivedCheque`
|
||||
// types immediately after each call returns, so every other file in this app can keep comparing
|
||||
// against `ChequeBookStatus.Active` etc. exactly as before.
|
||||
type RawChequePage = Omit<ChequePage, "issueStatus" | "payeeType"> & {
|
||||
issueStatus: number
|
||||
payeeType: number | null
|
||||
}
|
||||
type RawChequeBook = Omit<ChequeBook, "status" | "pages"> & {
|
||||
status: number
|
||||
pages: RawChequePage[]
|
||||
}
|
||||
type RawReceivedCheque = Omit<ReceivedCheque, "receivedFromType" | "status"> & {
|
||||
receivedFromType: number
|
||||
status: number
|
||||
}
|
||||
|
||||
function mapChequePage(raw: RawChequePage): ChequePage {
|
||||
return {
|
||||
...raw,
|
||||
issueStatus: CHEQUE_PAGE_ISSUE_STATUS_BY_CODE[raw.issueStatus],
|
||||
payeeType: raw.payeeType == null ? null : PAYEE_TYPE_BY_CODE[raw.payeeType],
|
||||
}
|
||||
}
|
||||
|
||||
function mapChequeBook(raw: RawChequeBook): ChequeBook {
|
||||
return {
|
||||
...raw,
|
||||
status: CHEQUE_BOOK_STATUS_BY_CODE[raw.status],
|
||||
pages: (raw.pages ?? []).map(mapChequePage),
|
||||
}
|
||||
}
|
||||
|
||||
function mapReceivedCheque(raw: RawReceivedCheque): ReceivedCheque {
|
||||
return {
|
||||
...raw,
|
||||
receivedFromType: RECEIVED_FROM_TYPE_BY_CODE[raw.receivedFromType],
|
||||
status: RECEIVED_CHEQUE_STATUS_BY_CODE[raw.status],
|
||||
}
|
||||
}
|
||||
|
||||
export const chequeBooksApi = {
|
||||
list(params?: {
|
||||
async list(params?: {
|
||||
bankAccountId?: number
|
||||
branchId?: number
|
||||
status?: ChequeBookStatus
|
||||
page?: number
|
||||
pageSize?: number
|
||||
}): Promise<GlPagedResult<ChequeBook>> {
|
||||
return glRequest<GlPagedResult<ChequeBook>>("/cheque-books", { query: { ...params } })
|
||||
const res = await glRequest<GlPagedResult<RawChequeBook>>("/cheque-books", { query: { ...params } })
|
||||
return { ...res, items: res.items.map(mapChequeBook) }
|
||||
},
|
||||
|
||||
/** `expandPages` maps to GL's `?expand=pages` — omit it for just the book's own fields. */
|
||||
get(chequeBookNo: string, expandPages = false): Promise<ChequeBook> {
|
||||
return glRequest<ChequeBook>(`/cheque-books/${encodeURIComponent(chequeBookNo)}`, {
|
||||
async get(chequeBookNo: string, expandPages = false): Promise<ChequeBook> {
|
||||
const res = await glRequest<RawChequeBook>(`/cheque-books/${encodeURIComponent(chequeBookNo)}`, {
|
||||
query: expandPages ? { expand: "pages" } : undefined,
|
||||
})
|
||||
return mapChequeBook(res)
|
||||
},
|
||||
|
||||
/** Auto-generates every leaf (`totalLeaves` `ChequePage` rows, all `Unused`) in the same call — the response's `pages[]` already has them. */
|
||||
create(request: CreateChequeBookRequest): Promise<ChequeBook> {
|
||||
return glRequest<ChequeBook>("/cheque-books", { method: "POST", body: request })
|
||||
async create(request: CreateChequeBookRequest): Promise<ChequeBook> {
|
||||
const res = await glRequest<RawChequeBook>("/cheque-books", { method: "POST", body: request })
|
||||
return mapChequeBook(res)
|
||||
},
|
||||
}
|
||||
|
||||
export const chequePagesApi = {
|
||||
issue(chequeNo: string, request: IssueChequePageRequest): Promise<ChequePage> {
|
||||
return glRequest<ChequePage>(`/cheque-pages/${encodeURIComponent(chequeNo)}/issue`, {
|
||||
async issue(chequeNo: string, request: IssueChequePageRequest): Promise<ChequePage> {
|
||||
const res = await glRequest<RawChequePage>(`/cheque-pages/${encodeURIComponent(chequeNo)}/issue`, {
|
||||
method: "PUT",
|
||||
body: request,
|
||||
})
|
||||
return mapChequePage(res)
|
||||
},
|
||||
|
||||
/** `Clear`/`Bounce`/`Cancel`/`Void` — only valid from certain `issueStatus` values, see `types/general-ledger.ts`'s `ChequePageStatusAction`. */
|
||||
updateStatus(chequeNo: string, request: UpdateChequePageStatusRequest): Promise<ChequePage> {
|
||||
return glRequest<ChequePage>(`/cheque-pages/${encodeURIComponent(chequeNo)}/status`, {
|
||||
async updateStatus(chequeNo: string, request: UpdateChequePageStatusRequest): Promise<ChequePage> {
|
||||
const res = await glRequest<RawChequePage>(`/cheque-pages/${encodeURIComponent(chequeNo)}/status`, {
|
||||
method: "PUT",
|
||||
body: request,
|
||||
})
|
||||
return mapChequePage(res)
|
||||
},
|
||||
}
|
||||
|
||||
/** Received Cheques — cheques received from customers/suppliers/others, deliberately unlinked to any `ChequeBook`. */
|
||||
export const receivedChequesApi = {
|
||||
list(params?: {
|
||||
async list(params?: {
|
||||
companyId?: number
|
||||
branchId?: number
|
||||
status?: ReceivedChequeStatus
|
||||
@@ -308,15 +363,18 @@ export const receivedChequesApi = {
|
||||
page?: number
|
||||
pageSize?: number
|
||||
}): Promise<GlPagedResult<ReceivedCheque>> {
|
||||
return glRequest<GlPagedResult<ReceivedCheque>>("/received-cheques", { query: { ...params } })
|
||||
const res = await glRequest<GlPagedResult<RawReceivedCheque>>("/received-cheques", { query: { ...params } })
|
||||
return { ...res, items: res.items.map(mapReceivedCheque) }
|
||||
},
|
||||
|
||||
create(request: CreateReceivedChequeRequest): Promise<ReceivedCheque> {
|
||||
return glRequest<ReceivedCheque>("/received-cheques", { method: "POST", body: request })
|
||||
async create(request: CreateReceivedChequeRequest): Promise<ReceivedCheque> {
|
||||
const res = await glRequest<RawReceivedCheque>("/received-cheques", { method: "POST", body: request })
|
||||
return mapReceivedCheque(res)
|
||||
},
|
||||
|
||||
/** `Deposit`/`Clear`/`Return`/`Cancel` — only valid from certain statuses, see `types/general-ledger.ts`'s `ReceivedChequeStatusAction`. */
|
||||
updateStatus(id: number, request: UpdateReceivedChequeStatusRequest): Promise<ReceivedCheque> {
|
||||
return glRequest<ReceivedCheque>(`/received-cheques/${id}/status`, { method: "PUT", body: request })
|
||||
async updateStatus(id: number, request: UpdateReceivedChequeStatusRequest): Promise<ReceivedCheque> {
|
||||
const res = await glRequest<RawReceivedCheque>(`/received-cheques/${id}/status`, { method: "PUT", body: request })
|
||||
return mapReceivedCheque(res)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -410,6 +410,59 @@ export enum ReceivedChequeStatusAction {
|
||||
Cancel = "Cancel",
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirmed (`06_Enums_Reference.md`, user-supplied) — GL has **no global `JsonStringEnumConverter`**
|
||||
* registered. That's documented there as a request-body binding gotcha (a JSON-body enum field must
|
||||
* be sent as its string name, parsed server-side via `Enum.TryParse`), but the same missing converter
|
||||
* also governs the other direction: every one of these five fields is a real enum-typed property on
|
||||
* GL's own response DTO (backed by an `integer` DB column, per that doc's "Persisted enums" table),
|
||||
* so with no converter registered, GL's JSON response serializes each one as its **raw integer**
|
||||
* (`1`/`2`/`3`/...), not the name — confirmed live by the user ("most status has integer numbers").
|
||||
* Request bodies/query-string filters are unaffected and still take the string name as before (a
|
||||
* JSON-body enum field is independently declared `string` server-side, and query-string enum
|
||||
* binding parses names natively) — only inbound response values need translating. These maps do
|
||||
* that translation, keyed by the exact integer values `06_Enums_Reference.md` documents; applied in
|
||||
* `lib/api/general-ledger.ts` immediately after each GL call returns, so every consumer of
|
||||
* `ChequeBook`/`ChequePage`/`ReceivedCheque` in this frontend keeps working with the same string
|
||||
* enum values as before and never has to know GL sent a number.
|
||||
*/
|
||||
export const CHEQUE_BOOK_STATUS_BY_CODE: Record<number, ChequeBookStatus> = {
|
||||
1: ChequeBookStatus.Active,
|
||||
2: ChequeBookStatus.Completed,
|
||||
3: ChequeBookStatus.Cancelled,
|
||||
}
|
||||
|
||||
export const CHEQUE_PAGE_ISSUE_STATUS_BY_CODE: Record<number, ChequePageIssueStatus> = {
|
||||
1: ChequePageIssueStatus.Unused,
|
||||
2: ChequePageIssueStatus.Issued,
|
||||
3: ChequePageIssueStatus.Cleared,
|
||||
4: ChequePageIssueStatus.Bounced,
|
||||
5: ChequePageIssueStatus.Cancelled,
|
||||
6: ChequePageIssueStatus.Void,
|
||||
}
|
||||
|
||||
/** `ChequePage.payeeType` is nullable — only set once a page is issued (`06_Enums_Reference.md`). */
|
||||
export const PAYEE_TYPE_BY_CODE: Record<number, PayeeType> = {
|
||||
1: PayeeType.Supplier,
|
||||
2: PayeeType.Customer,
|
||||
3: PayeeType.Employee,
|
||||
4: PayeeType.Other,
|
||||
}
|
||||
|
||||
export const RECEIVED_FROM_TYPE_BY_CODE: Record<number, ReceivedFromType> = {
|
||||
1: ReceivedFromType.Customer,
|
||||
2: ReceivedFromType.Supplier,
|
||||
3: ReceivedFromType.Other,
|
||||
}
|
||||
|
||||
export const RECEIVED_CHEQUE_STATUS_BY_CODE: Record<number, ReceivedChequeStatus> = {
|
||||
1: ReceivedChequeStatus.Received,
|
||||
2: ReceivedChequeStatus.Deposited,
|
||||
3: ReceivedChequeStatus.Cleared,
|
||||
4: ReceivedChequeStatus.Returned,
|
||||
5: ReceivedChequeStatus.Cancelled,
|
||||
}
|
||||
|
||||
/**
|
||||
* A single leaf of a Cheque Book. GL's own reference confirms every field named in the `issue`
|
||||
* request body plus `issueStatus`/`printedAt`/`clearedDate`/`clearedByBank`/`cancelReason` in
|
||||
|
||||
Reference in New Issue
Block a user