feat: maintenance log (#170)

* remove repo for document tokens

* remove schema for doc tokens

* fix id template and generate cmd

* schema updates

* code gen

* bump dependencies

* fix broken migrations + add maintenance entry type

* spelling

* remove debug logger

* implement repository layer

* routes

* API client

* wip: maintenance log

* remove depreciated call
This commit is contained in:
Hayden 2022-12-09 20:57:57 -09:00 committed by GitHub
parent d6da63187b
commit 5bbb969763
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
79 changed files with 6320 additions and 4957 deletions

View file

@ -33,6 +33,7 @@ describe("user should be able to create an item and add an attachment", () => {
const [location, cleanup] = await useLocation(api);
const { response, data: item } = await api.items.create({
parentId: null,
name: "test-item",
labelIds: [],
description: "test-description",
@ -43,7 +44,7 @@ describe("user should be able to create an item and add an attachment", () => {
// Add attachment
{
const testFile = new Blob(["test"], { type: "text/plain" });
const { response } = await api.items.addAttachment(item.id, testFile, "test.txt", AttachmentTypes.Attachment);
const { response } = await api.items.attachments.add(item.id, testFile, "test.txt", AttachmentTypes.Attachment);
expect(response.status).toBe(201);
}
@ -54,7 +55,7 @@ describe("user should be able to create an item and add an attachment", () => {
expect(data.attachments).toHaveLength(1);
expect(data.attachments[0].document.title).toBe("test.txt");
const resp = await api.items.deleteAttachment(data.id, data.attachments[0].id);
const resp = await api.items.attachments.delete(data.id, data.attachments[0].id);
expect(resp.response.status).toBe(204);
api.items.delete(item.id);
@ -66,6 +67,7 @@ describe("user should be able to create an item and add an attachment", () => {
const [location, cleanup] = await useLocation(api);
const { response, data: item } = await api.items.create({
parentId: null,
name: faker.vehicle.model(),
labelIds: [],
description: faker.lorem.paragraph(1),
@ -82,6 +84,7 @@ describe("user should be able to create an item and add an attachment", () => {
// Add fields
const itemUpdate = {
parentId: null,
...item,
locationId: item.location.id,
labelIds: item.labels.map(l => l.id),
@ -113,4 +116,41 @@ describe("user should be able to create an item and add an attachment", () => {
cleanup();
});
test("users should be able to create and few maintenance logs for an item", async () => {
const api = await sharedUserClient();
const [location, cleanup] = await useLocation(api);
const { response, data: item } = await api.items.create({
parentId: null,
name: faker.vehicle.model(),
labelIds: [],
description: faker.lorem.paragraph(1),
locationId: location.id,
});
expect(response.status).toBe(201);
const maintenanceEntries = [];
for (let i = 0; i < 5; i++) {
const { response, data } = await api.items.maintenance.create(item.id, {
name: faker.vehicle.model(),
description: faker.lorem.paragraph(1),
date: faker.date.past(1),
cost: faker.datatype.number(100).toString(),
});
expect(response.status).toBe(201);
maintenanceEntries.push(data);
}
// Log
{
const { response, data } = await api.items.maintenance.getLog(item.id);
expect(response.status).toBe(200);
expect(data.entries).toHaveLength(maintenanceEntries.length);
expect(data.costAverage).toBeGreaterThan(0);
expect(data.costTotal).toBeGreaterThan(0);
}
cleanup();
});
});

View file

@ -1,7 +1,18 @@
import { BaseAPI, route } from "../base";
import { parseDate } from "../base/base-api";
import { ItemAttachmentUpdate, ItemCreate, ItemOut, ItemSummary, ItemUpdate } from "../types/data-contracts";
import {
ItemAttachmentUpdate,
ItemCreate,
ItemOut,
ItemSummary,
ItemUpdate,
MaintenanceEntry,
MaintenanceEntryCreate,
MaintenanceEntryUpdate,
MaintenanceLog,
} from "../types/data-contracts";
import { AttachmentTypes, PaginationResult } from "../types/non-generated";
import { Requests } from "~~/lib/requests";
export type ItemsQuery = {
includeArchived?: boolean;
@ -12,7 +23,65 @@ export type ItemsQuery = {
q?: string;
};
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,
});
}
}
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,
});
}
}
export class ItemsApi extends BaseAPI {
attachments: AttachmentsAPI;
maintenance: MaintenanceAPI;
constructor(http: Requests, token: string) {
super(http, token);
this.attachments = new AttachmentsAPI(http);
this.maintenance = new MaintenanceAPI(http);
}
getAll(q: ItemsQuery = {}) {
return this.http.get<PaginationResult<ItemSummary>>({ url: route("/items", q) });
}
@ -59,27 +128,4 @@ export class ItemsApi extends BaseAPI {
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 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,
});
}
}

View file

@ -54,7 +54,6 @@ export interface ItemAttachmentUpdate {
export interface ItemCreate {
description: string;
labelIds: string[];
/** Edges */
locationId: string;
name: string;
@ -73,8 +72,7 @@ export interface ItemField {
export interface ItemOut {
archived: boolean;
/** @example 0 */
/** @example "0" */
assetId: string;
attachments: ItemAttachment[];
children: ItemSummary[];
@ -84,33 +82,26 @@ export interface ItemOut {
id: string;
insured: boolean;
labels: LabelSummary[];
/** Warranty */
lifetimeWarranty: boolean;
/** Edges */
location: LocationSummary | null;
manufacturer: string;
modelNumber: string;
name: string;
/** Extras */
notes: string;
parent: ItemSummary | null;
purchaseFrom: string;
/** @example 0 */
/** @example "0" */
purchasePrice: string;
/** Purchase */
purchaseTime: Date;
quantity: number;
serialNumber: string;
soldNotes: string;
/** @example 0 */
/** @example "0" */
soldPrice: string;
/** Sold */
soldTime: Date;
soldTo: string;
@ -126,7 +117,6 @@ export interface ItemSummary {
id: string;
insured: boolean;
labels: LabelSummary[];
/** Edges */
location: LocationSummary | null;
name: string;
@ -142,35 +132,27 @@ export interface ItemUpdate {
id: string;
insured: boolean;
labelIds: string[];
/** Warranty */
lifetimeWarranty: boolean;
/** Edges */
locationId: string;
manufacturer: string;
modelNumber: string;
name: string;
/** Extras */
notes: string;
parentId: string | null;
purchaseFrom: string;
/** @example 0 */
/** @example "0" */
purchasePrice: string;
/** Purchase */
purchaseTime: Date;
quantity: number;
/** Identifications */
serialNumber: string;
soldNotes: string;
/** @example 0 */
/** @example "0" */
soldPrice: string;
/** Sold */
soldTime: Date;
soldTo: string;
@ -241,6 +223,38 @@ export interface LocationUpdate {
parentId: string | null;
}
export interface MaintenanceEntry {
/** @example "0" */
cost: string;
date: Date;
description: string;
id: string;
name: string;
}
export interface MaintenanceEntryCreate {
/** @example "0" */
cost: string;
date: Date;
description: string;
name: string;
}
export interface MaintenanceEntryUpdate {
/** @example "0" */
cost: string;
date: Date;
description: string;
name: string;
}
export interface MaintenanceLog {
costAverage: number;
costTotal: number;
entries: MaintenanceEntry[];
itemId: string;
}
export interface PaginationResultRepoItemSummary {
items: ItemSummary[];
page: number;
@ -278,7 +292,7 @@ export interface ValueOverTime {
}
export interface ValueOverTimeEntry {
date: string;
date: Date;
name: string;
value: number;
}