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-09-24 19:33:38 +00:00
|
|
|
import {
|
|
|
|
ItemAttachmentToken,
|
|
|
|
ItemAttachmentUpdate,
|
|
|
|
ItemCreate,
|
|
|
|
ItemOut,
|
|
|
|
ItemSummary,
|
|
|
|
ItemUpdate,
|
|
|
|
} from "../types/data-contracts";
|
2022-10-13 05:13:07 +00:00
|
|
|
import { AttachmentTypes, PaginationResult } from "../types/non-generated";
|
|
|
|
|
|
|
|
export type ItemsQuery = {
|
|
|
|
page?: number;
|
|
|
|
pageSize?: number;
|
|
|
|
locations?: string[];
|
|
|
|
labels?: string[];
|
|
|
|
q?: string;
|
|
|
|
};
|
2022-09-03 09:17:57 +00:00
|
|
|
|
|
|
|
export class ItemsApi extends BaseAPI {
|
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-09-09 22:46:53 +00:00
|
|
|
import(file: File) {
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addAttachment(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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAttachmentUrl(id: string, attachmentId: string): Promise<string> {
|
|
|
|
const payload = await this.http.get<ItemAttachmentToken>({
|
|
|
|
url: route(`/items/${id}/attachments/${attachmentId}`),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!payload.data) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return route(`/items/${id}/attachments/download`, { token: payload.data.token });
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteAttachment(id: string, attachmentId: string) {
|
|
|
|
return await this.http.delete<void>({ url: route(`/items/${id}/attachments/${attachmentId}`) });
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateAttachment(id: string, attachmentId: string, data: ItemAttachmentUpdate) {
|
|
|
|
return await this.http.put<ItemAttachmentUpdate, ItemOut>({
|
|
|
|
url: route(`/items/${id}/attachments/${attachmentId}`),
|
|
|
|
body: data,
|
|
|
|
});
|
2022-09-03 09:17:57 +00:00
|
|
|
}
|
|
|
|
}
|