2022-09-09 22:46:53 +00:00
|
|
|
import { BaseAPI, route } from "../base";
|
2022-09-12 22:47:27 +00:00
|
|
|
import { parseDate } from "../base/base-api";
|
2022-12-10 05:57:57 +00:00
|
|
|
import {
|
|
|
|
ItemAttachmentUpdate,
|
|
|
|
ItemCreate,
|
|
|
|
ItemOut,
|
|
|
|
ItemSummary,
|
|
|
|
ItemUpdate,
|
|
|
|
MaintenanceEntry,
|
|
|
|
MaintenanceEntryCreate,
|
|
|
|
MaintenanceEntryUpdate,
|
|
|
|
MaintenanceLog,
|
|
|
|
} from "../types/data-contracts";
|
2022-10-13 05:13:07 +00:00
|
|
|
import { AttachmentTypes, PaginationResult } from "../types/non-generated";
|
2022-12-10 05:57:57 +00:00
|
|
|
import { Requests } from "~~/lib/requests";
|
2022-10-13 05:13:07 +00:00
|
|
|
|
|
|
|
export type ItemsQuery = {
|
2022-11-01 07:30:42 +00:00
|
|
|
includeArchived?: boolean;
|
2022-10-13 05:13:07 +00:00
|
|
|
page?: number;
|
|
|
|
pageSize?: number;
|
|
|
|
locations?: string[];
|
|
|
|
labels?: string[];
|
|
|
|
q?: string;
|
2023-02-05 21:12:54 +00:00
|
|
|
fields?: string[];
|
2022-10-13 05:13:07 +00:00
|
|
|
};
|
2022-09-03 09:17:57 +00:00
|
|
|
|
2022-12-10 05:57:57 +00:00
|
|
|
export class AttachmentsAPI extends BaseAPI {
|
|
|
|
add(id: string, file: File | Blob, filename: string, type: AttachmentTypes) {
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("file", file);
|
|
|
|
formData.append("type", type);
|
|
|
|
formData.append("name", filename);
|
|
|
|
|
|
|
|
return this.http.post<FormData, ItemOut>({
|
|
|
|
url: route(`/items/${id}/attachments`),
|
|
|
|
data: formData,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(id: string, attachmentId: string) {
|
|
|
|
return this.http.delete<void>({ url: route(`/items/${id}/attachments/${attachmentId}`) });
|
|
|
|
}
|
|
|
|
|
|
|
|
update(id: string, attachmentId: string, data: ItemAttachmentUpdate) {
|
|
|
|
return this.http.put<ItemAttachmentUpdate, ItemOut>({
|
|
|
|
url: route(`/items/${id}/attachments/${attachmentId}`),
|
|
|
|
body: data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 21:12:54 +00:00
|
|
|
export class FieldsAPI extends BaseAPI {
|
|
|
|
getAll() {
|
|
|
|
return this.http.get<string[]>({ url: route("/items/fields") });
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllValues(field: string) {
|
|
|
|
return this.http.get<string[]>({ url: route(`/items/fields/values`, { field }) });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 05:57:57 +00:00
|
|
|
export class MaintenanceAPI extends BaseAPI {
|
|
|
|
getLog(itemId: string) {
|
|
|
|
return this.http.get<MaintenanceLog>({ url: route(`/items/${itemId}/maintenance`) });
|
|
|
|
}
|
|
|
|
|
|
|
|
create(itemId: string, data: MaintenanceEntryCreate) {
|
|
|
|
return this.http.post<MaintenanceEntryCreate, MaintenanceEntry>({
|
|
|
|
url: route(`/items/${itemId}/maintenance`),
|
|
|
|
body: data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(itemId: string, entryId: string) {
|
|
|
|
return this.http.delete<void>({ url: route(`/items/${itemId}/maintenance/${entryId}`) });
|
|
|
|
}
|
|
|
|
|
|
|
|
update(itemId: string, entryId: string, data: MaintenanceEntryUpdate) {
|
|
|
|
return this.http.put<MaintenanceEntryUpdate, MaintenanceEntry>({
|
|
|
|
url: route(`/items/${itemId}/maintenance/${entryId}`),
|
|
|
|
body: data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-03 09:17:57 +00:00
|
|
|
export class ItemsApi extends BaseAPI {
|
2022-12-10 05:57:57 +00:00
|
|
|
attachments: AttachmentsAPI;
|
|
|
|
maintenance: MaintenanceAPI;
|
2023-02-05 21:12:54 +00:00
|
|
|
fields: FieldsAPI;
|
2022-12-10 05:57:57 +00:00
|
|
|
|
|
|
|
constructor(http: Requests, token: string) {
|
|
|
|
super(http, token);
|
2023-02-05 21:12:54 +00:00
|
|
|
this.fields = new FieldsAPI(http);
|
2022-12-10 05:57:57 +00:00
|
|
|
this.attachments = new AttachmentsAPI(http);
|
|
|
|
this.maintenance = new MaintenanceAPI(http);
|
|
|
|
}
|
|
|
|
|
2022-10-13 05:13:07 +00:00
|
|
|
getAll(q: ItemsQuery = {}) {
|
|
|
|
return this.http.get<PaginationResult<ItemSummary>>({ url: route("/items", q) });
|
2022-09-03 09:17:57 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
create(item: ItemCreate) {
|
2022-09-24 19:33:38 +00:00
|
|
|
return this.http.post<ItemCreate, ItemOut>({ url: route("/items"), body: item });
|
2022-09-03 09:17:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async get(id: string) {
|
2022-09-12 22:47:27 +00:00
|
|
|
const payload = await this.http.get<ItemOut>({ url: route(`/items/${id}`) });
|
2022-09-07 05:58:59 +00:00
|
|
|
|
|
|
|
if (!payload.data) {
|
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse Date Types
|
2022-09-12 22:47:27 +00:00
|
|
|
payload.data = parseDate(payload.data, ["purchaseTime", "soldTime", "warrantyExpires"]);
|
2022-09-07 05:58:59 +00:00
|
|
|
return payload;
|
2022-09-03 09:17:57 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
delete(id: string) {
|
2022-09-09 06:05:23 +00:00
|
|
|
return this.http.delete<void>({ url: route(`/items/${id}`) });
|
2022-09-03 09:17:57 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
async update(id: string, item: ItemUpdate) {
|
|
|
|
const payload = await this.http.put<ItemCreate, ItemOut>({
|
|
|
|
url: route(`/items/${id}`),
|
|
|
|
body: this.dropFields(item),
|
|
|
|
});
|
|
|
|
if (!payload.data) {
|
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
payload.data = parseDate(payload.data, ["purchaseTime", "soldTime", "warrantyExpires"]);
|
|
|
|
return payload;
|
2022-09-09 06:05:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 21:36:32 +00:00
|
|
|
import(file: File | Blob) {
|
2022-09-09 06:05:23 +00:00
|
|
|
const formData = new FormData();
|
2022-09-09 22:46:53 +00:00
|
|
|
formData.append("csv", file);
|
2022-09-09 06:05:23 +00:00
|
|
|
|
2022-09-24 19:33:38 +00:00
|
|
|
return this.http.post<FormData, void>({
|
|
|
|
url: route("/items/import"),
|
|
|
|
data: formData,
|
|
|
|
});
|
|
|
|
}
|
2022-09-03 09:17:57 +00:00
|
|
|
}
|