mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-07 10:28:59 +00:00
feat(work-orders): fetch Extra Docs via authenticated content GET
This commit is contained in:
parent
599e38aae3
commit
edf4a51d4c
2 changed files with 59 additions and 0 deletions
|
|
@ -199,6 +199,27 @@ export const workOrderBoardDocumentsApi = {
|
||||||
throwDocumentHttpError(response, data);
|
throwDocumentHttpError(response, data);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getMediaContent: async (
|
||||||
|
workOrderId: string | number,
|
||||||
|
mediaId: string | number,
|
||||||
|
): Promise<Blob> => {
|
||||||
|
const response = await api.get(API_PATHS.workOrder.mediaContent(workOrderId, mediaId), {
|
||||||
|
credentials: "include",
|
||||||
|
throwHttpErrors: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
return response.blob();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.status === 404) {
|
||||||
|
throw new ApiError(mapHttpStatusToMessage(404), 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await readResponseJson(response);
|
||||||
|
throwDocumentHttpError(response, data);
|
||||||
|
},
|
||||||
|
|
||||||
uploadCompletionDoc: async (
|
uploadCompletionDoc: async (
|
||||||
workOrderId: string | number,
|
workOrderId: string | number,
|
||||||
params: {
|
params: {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ const apiPatch = vi.fn();
|
||||||
const apiPatchFn = vi.fn();
|
const apiPatchFn = vi.fn();
|
||||||
const apiPostFn = vi.fn();
|
const apiPostFn = vi.fn();
|
||||||
const apiDeleteFn = vi.fn();
|
const apiDeleteFn = vi.fn();
|
||||||
|
const apiGetFn = vi.fn();
|
||||||
|
|
||||||
vi.mock("@/api/api", () => ({
|
vi.mock("@/api/api", () => ({
|
||||||
apiGet: (...args: unknown[]) => apiGet(...args),
|
apiGet: (...args: unknown[]) => apiGet(...args),
|
||||||
|
|
@ -902,3 +903,40 @@ describe("API_PATHS.workOrder.mediaContent", () => {
|
||||||
expect(API_PATHS.workOrder.mediaContent(10, 12)).toBe("workorders/10/media/12/content");
|
expect(API_PATHS.workOrder.mediaContent(10, 12)).toBe("workorders/10/media/12/content");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("workOrdersApi.getMediaContent", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
apiGetFn.mockReset();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns the response blob on 200", async () => {
|
||||||
|
const blob = new Blob(["pdf-bytes"], { type: "application/pdf" });
|
||||||
|
apiGetFn.mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
status: 200,
|
||||||
|
blob: async () => blob,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await workOrdersApi.getMediaContent(10, 12);
|
||||||
|
|
||||||
|
expect(result).toBe(blob);
|
||||||
|
expect(apiGetFn).toHaveBeenCalledWith(
|
||||||
|
API_PATHS.workOrder.mediaContent(10, 12),
|
||||||
|
expect.objectContaining({ credentials: "include", throwHttpErrors: false }),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws ApiError on 404 without inventing a filename", async () => {
|
||||||
|
apiGetFn.mockResolvedValue({
|
||||||
|
ok: false,
|
||||||
|
status: 404,
|
||||||
|
json: async () => ({ fileName: "secret.pdf" }),
|
||||||
|
});
|
||||||
|
|
||||||
|
await expect(workOrdersApi.getMediaContent(10, 12)).rejects.toMatchObject({
|
||||||
|
name: "ApiError",
|
||||||
|
status: 404,
|
||||||
|
message: "The requested resource was not found.",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue