feat: item-attachments CRUD (#22)

* change /content/ -> /homebox/

* add cache to code generators

* update env variables to set data storage

* update env variables

* set env variables in prod container

* implement attachment post route (WIP)

* get attachment endpoint

* attachment download

* implement string utilities lib

* implement generic drop zone

* use explicit truncate

* remove clean dir

* drop strings composable for lib

* update item types and add attachments

* add attachment API

* implement service context

* consolidate API code

* implement editing attachments

* implement upload limit configuration

* improve error handling

* add docs for max upload size

* fix test cases
This commit is contained in:
Hayden 2022-09-24 11:33:38 -08:00 committed by GitHub
parent 852d312ba7
commit 31b34241e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
165 changed files with 2509 additions and 664 deletions

View file

@ -0,0 +1,61 @@
import { describe, test, expect } from "vitest";
import { LocationOut } from "../../types/data-contracts";
import { AttachmentTypes } from "../../types/non-generated";
import { UserApi } from "../../user";
import { sharedUserClient } from "../test-utils";
describe("user should be able to create an item and add an attachment", () => {
let increment = 0;
/**
* useLocatio sets up a location resource for testing, and returns a function
* that can be used to delete the location from the backend server.
*/
async function useLocation(api: UserApi): Promise<[LocationOut, () => Promise<void>]> {
const { response, data } = await api.locations.create({
name: `__test__.location.name_${increment}`,
description: `__test__.location.description_${increment}`,
});
expect(response.status).toBe(201);
increment++;
const cleanup = async () => {
const { response } = await api.locations.delete(data.id);
expect(response.status).toBe(204);
};
return [data, cleanup];
}
test("user should be able to create an item and add an attachment", async () => {
const api = await sharedUserClient();
const [location, cleanup] = await useLocation(api);
const { response, data: item } = await api.items.create({
name: "test-item",
labelIds: [],
description: "test-description",
locationId: location.id,
});
expect(response.status).toBe(201);
// Add attachment
{
const testFile = new Blob(["test"], { type: "text/plain" });
const { response } = await api.items.addAttachment(item.id, testFile, "test.txt", AttachmentTypes.Attachment);
expect(response.status).toBe(201);
}
// Get Attachment
const { response: itmResp, data } = await api.items.get(item.id);
expect(itmResp.status).toBe(200);
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);
expect(resp.response.status).toBe(204);
api.items.delete(item.id);
await cleanup();
});
});

View file

@ -1,15 +1,23 @@
import { BaseAPI, route } from "../base";
import { parseDate } from "../base/base-api";
import { ItemCreate, ItemOut, ItemSummary, ItemUpdate } from "../types/data-contracts";
import {
ItemAttachmentToken,
ItemAttachmentUpdate,
ItemCreate,
ItemOut,
ItemSummary,
ItemUpdate,
} from "../types/data-contracts";
import { AttachmentTypes } from "../types/non-generated";
import { Results } from "./types";
export class ItemsApi extends BaseAPI {
getAll() {
return this.http.get<Results<ItemOut>>({ url: route("/items") });
return this.http.get<Results<ItemSummary>>({ url: route("/items") });
}
create(item: ItemCreate) {
return this.http.post<ItemCreate, ItemSummary>({ url: route("/items"), body: item });
return this.http.post<ItemCreate, ItemOut>({ url: route("/items"), body: item });
}
async get(id: string) {
@ -45,6 +53,44 @@ export class ItemsApi extends BaseAPI {
const formData = new FormData();
formData.append("csv", file);
return this.http.post<FormData, void>({ url: route("/items/import"), data: formData });
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,
});
}
}

View file

@ -21,6 +21,11 @@ export interface ServerResults {
items: any;
}
export interface ServerValidationError {
field: string;
reason: string;
}
export interface ApiSummary {
build: Build;
health: boolean;
@ -45,9 +50,19 @@ export interface ItemAttachment {
createdAt: Date;
document: DocumentOut;
id: string;
type: string;
updatedAt: Date;
}
export interface ItemAttachmentToken {
token: string;
}
export interface ItemAttachmentUpdate {
title: string;
type: string;
}
export interface ItemCreate {
description: string;
labelIds: string[];
@ -191,7 +206,6 @@ export interface LabelCreate {
export interface LabelOut {
createdAt: Date;
description: string;
groupId: string;
id: string;
items: ItemSummary[];
name: string;
@ -201,7 +215,6 @@ export interface LabelOut {
export interface LabelSummary {
createdAt: Date;
description: string;
groupId: string;
id: string;
name: string;
updatedAt: Date;

View file

@ -0,0 +1,6 @@
export enum AttachmentTypes {
Photo = "photo",
Manual = "manual",
Warranty = "warranty",
Attachment = "attachment",
}