refactor: repositories (#28)

* cleanup unnecessary mocks

* refactor document storage location

* remove unused function

* move ownership to document types to repo package

* move types and mappers to repo package

* refactor sets to own package
This commit is contained in:
Hayden 2022-09-27 15:52:13 -08:00 committed by GitHub
parent 2e82398e5c
commit 343290a55a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
79 changed files with 3169 additions and 3160 deletions

View file

@ -26,11 +26,11 @@
</template>
<script lang="ts" setup>
import { LocationCount } from "~~/lib/api/types/data-contracts";
import { LocationOutCount } from "~~/lib/api/types/data-contracts";
defineProps({
location: {
type: Object as () => LocationCount,
type: Object as () => LocationOutCount,
required: true,
},
dense: {

View file

@ -5,7 +5,7 @@ import { Ref } from "vue";
type Store = UseConfirmDialogReturn<any, boolean, boolean> & {
text: Ref<string>;
setup: boolean;
open: (text: string) => Promise<UseConfirmDialogRevealResult<boolean, boolean>>;
open: (text: string) => Promise<UseConfirmDialogRevealResult<boolean, boolean>>;
};
const store: Partial<Store> = {
@ -31,7 +31,7 @@ export function useConfirm(): Store {
store.cancel = cancel;
}
async function openDialog(msg: string): Promise<UseConfirmDialogRevealResult<boolean, boolean>> {
async function openDialog(msg: string): Promise<UseConfirmDialogRevealResult<boolean, boolean>> {
store.text.value = msg;
return await store.reveal();
}

View file

@ -14,11 +14,9 @@ describe("first time user workflow (register, login)", () => {
const api = client();
const userData = {
groupName: "test-group",
user: {
email: "test-user@email.com",
name: "test-user",
password: "test-password",
},
email: "test-user@email.com",
name: "test-user",
password: "test-password",
};
test("user should be able to register", async () => {
@ -27,7 +25,7 @@ describe("first time user workflow (register, login)", () => {
});
test("user should be able to login", async () => {
const { response, data } = await api.login(userData.user.email, userData.user.password);
const { response, data } = await api.login(userData.email, userData.password);
expect(response.status).toBe(200);
expect(data.token).toBeTruthy();

View file

@ -31,15 +31,13 @@ export async function sharedUserClient(): Promise<UserApi> {
}
const testUser = {
groupName: "test-group",
user: {
email: "__test__@__test__.com",
name: "__test__",
password: "__test__",
},
email: "__test__@__test__.com",
name: "__test__",
password: "__test__",
};
const api = client();
const { response: tryLoginResp, data } = await api.login(testUser.user.email, testUser.user.password);
const { response: tryLoginResp, data } = await api.login(testUser.email, testUser.password);
if (tryLoginResp.status === 200) {
cache.token = data.token;
@ -49,7 +47,7 @@ export async function sharedUserClient(): Promise<UserApi> {
const { response: registerResp } = await api.register(testUser);
expect(registerResp.status).toBe(204);
const { response: loginResp, data: loginData } = await api.login(testUser.user.email, testUser.user.password);
const { response: loginResp, data: loginData } = await api.login(testUser.email, testUser.password);
expect(loginResp.status).toBe(200);
cache.token = loginData.token;

View file

@ -1,12 +1,12 @@
import { BaseAPI, route } from "../base";
import { LocationCount, LocationCreate, LocationOut } from "../types/data-contracts";
import { LocationOutCount, LocationCreate, LocationOut } from "../types/data-contracts";
import { Results } from "./types";
export type LocationUpdate = LocationCreate;
export class LocationsApi extends BaseAPI {
getAll() {
return this.http.get<Results<LocationCount>>({ url: route("/locations") });
return this.http.get<Results<LocationOutCount>>({ url: route("/locations") });
}
create(body: LocationCreate) {

View file

@ -1,24 +1,11 @@
import { BaseAPI, route } from "./base";
export type LoginResult = {
token: string;
expiresAt: string;
};
import { ApiSummary, TokenResponse, UserRegistration } from "./types/data-contracts";
export type LoginPayload = {
username: string;
password: string;
};
export type RegisterPayload = {
user: {
email: string;
password: string;
name: string;
};
groupName: string;
};
export type StatusResult = {
health: boolean;
versions: string[];
@ -28,11 +15,11 @@ export type StatusResult = {
export class PublicApi extends BaseAPI {
public status() {
return this.http.get<StatusResult>({ url: route("/status") });
return this.http.get<ApiSummary>({ url: route("/status") });
}
public login(username: string, password: string) {
return this.http.post<LoginPayload, LoginResult>({
return this.http.post<LoginPayload, TokenResponse>({
url: route("/users/login"),
body: {
username,
@ -41,7 +28,7 @@ export class PublicApi extends BaseAPI {
});
}
public register(body: RegisterPayload) {
return this.http.post<RegisterPayload, LoginResult>({ url: route("/users/register"), body });
public register(body: UserRegistration) {
return this.http.post<UserRegistration, TokenResponse>({ url: route("/users/register"), body });
}
}

View file

@ -10,36 +10,6 @@
* ---------------------------------------------------------------
*/
export interface ServerResult {
details: any;
error: boolean;
item: any;
message: string;
}
export interface ServerResults {
items: any;
}
export interface ServerValidationError {
field: string;
reason: string;
}
export interface ApiSummary {
build: Build;
health: boolean;
message: string;
title: string;
versions: string[];
}
export interface Build {
buildTime: string;
commit: string;
version: string;
}
export interface DocumentOut {
id: string;
path: string;
@ -54,10 +24,6 @@ export interface ItemAttachment {
updatedAt: Date;
}
export interface ItemAttachmentToken {
token: string;
}
export interface ItemAttachmentUpdate {
title: string;
type: string;
@ -99,8 +65,6 @@ export interface ItemOut {
/** Purchase */
purchaseTime: Date;
quantity: number;
/** Identifications */
serialNumber: string;
soldNotes: string;
@ -122,39 +86,11 @@ export interface ItemSummary {
insured: boolean;
labels: LabelSummary[];
/** Warranty */
lifetimeWarranty: boolean;
/** Edges */
location: LocationSummary;
manufacturer: string;
modelNumber: string;
name: string;
/** Extras */
notes: string;
purchaseFrom: string;
/** @example 0 */
purchasePrice: string;
/** Purchase */
purchaseTime: Date;
quantity: number;
/** Identifications */
serialNumber: string;
soldNotes: string;
/** @example 0 */
soldPrice: string;
/** Sold */
soldTime: Date;
soldTo: string;
updatedAt: Date;
warrantyDetails: string;
warrantyExpires: Date;
}
export interface ItemUpdate {
@ -220,15 +156,6 @@ export interface LabelSummary {
updatedAt: Date;
}
export interface LocationCount {
createdAt: Date;
description: string;
id: string;
itemCount: number;
name: string;
updatedAt: Date;
}
export interface LocationCreate {
description: string;
name: string;
@ -243,6 +170,15 @@ export interface LocationOut {
updatedAt: Date;
}
export interface LocationOutCount {
createdAt: Date;
description: string;
id: string;
itemCount: number;
name: string;
updatedAt: Date;
}
export interface LocationSummary {
createdAt: Date;
description: string;
@ -251,17 +187,6 @@ export interface LocationSummary {
updatedAt: Date;
}
export interface TokenResponse {
expiresAt: string;
token: string;
}
export interface UserIn {
email: string;
name: string;
password: string;
}
export interface UserOut {
email: string;
groupId: string;
@ -271,12 +196,53 @@ export interface UserOut {
name: string;
}
export interface UserRegistration {
groupName: string;
user: UserIn;
}
export interface UserUpdate {
email: string;
name: string;
}
export interface ServerResult {
details: any;
error: boolean;
item: any;
message: string;
}
export interface ServerResults {
items: any;
}
export interface ServerValidationError {
field: string;
reason: string;
}
export interface UserRegistration {
email: string;
groupName: string;
name: string;
password: string;
}
export interface ApiSummary {
build: Build;
health: boolean;
message: string;
title: string;
versions: string[];
}
export interface Build {
buildTime: string;
commit: string;
version: string;
}
export interface ItemAttachmentToken {
token: string;
}
export interface TokenResponse {
expiresAt: string;
token: string;
}

View file

@ -25,11 +25,9 @@
async function registerUser() {
loading.value = true;
const { error } = await api.register({
user: {
name: username.value,
email: email.value,
password: password.value,
},
name: username.value,
email: email.value,
password: password.value,
groupName: groupName.value,
});

View file

@ -1,9 +1,9 @@
import { defineStore } from "pinia";
import { LocationCount } from "~~/lib/api/types/data-contracts";
import { LocationOutCount } from "~~/lib/api/types/data-contracts";
export const useLocationStore = defineStore("locations", {
state: () => ({
allLocations: null as LocationCount[] | null,
allLocations: null as LocationOutCount[] | null,
client: useUserApi(),
}),
getters: {
@ -12,7 +12,7 @@ export const useLocationStore = defineStore("locations", {
* synched with the server by intercepting the API calls and updating on the
* response
*/
locations(state): LocationCount[] {
locations(state): LocationOutCount[] {
if (state.allLocations === null) {
Promise.resolve(this.refresh());
}
@ -20,7 +20,7 @@ export const useLocationStore = defineStore("locations", {
},
},
actions: {
async refresh(): Promise<LocationCount[]> {
async refresh(): Promise<LocationOutCount[]> {
const result = await this.client.locations.getAll();
if (result.error) {
return result;