Skip to content

Commit f800917

Browse files
Expense and revenue feature + bulk line item mod (#217)
* done * lint
1 parent 6bf68f0 commit f800917

File tree

16 files changed

+292
-232
lines changed

16 files changed

+292
-232
lines changed

backend/src/modules/openapi/purchase-line-item.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ const getPurchaseLineItemsForPurchaseRoute = createRoute({
124124
const updatePurchaseLineItemCategoryRoute = createRoute({
125125
method: "patch",
126126
path: "/purchase/line/category",
127-
summary: "Updates a purchase line item's category",
128-
description: "Updates the category of the purchase line item with the given Id to the given category",
127+
summary: "Updates purchase line items' category",
128+
description: "Updates the category of the purchase line items with the given Ids to the given category",
129129
request: {
130130
body: {
131131
content: {
@@ -142,7 +142,7 @@ const updatePurchaseLineItemCategoryRoute = createRoute({
142142
schema: UpdatePurchaseLineItemResponseSchema,
143143
},
144144
},
145-
description: "Successfully updated the line item's category",
145+
description: "Successfully updated the line items' category",
146146
},
147147
...openApiErrorCodes("Error modifying purchase line item"),
148148
},
@@ -152,8 +152,8 @@ const updatePurchaseLineItemCategoryRoute = createRoute({
152152
const updatePurchaseLineItemTypeRoute = createRoute({
153153
method: "patch",
154154
path: "/purchase/line/type",
155-
summary: "Updates a purchase line item's type",
156-
description: "Updates the type of the purchase line item with the given Id to the given type",
155+
summary: "Updates a purchase line items' type",
156+
description: "Updates the type of the purchase line items with the given Ids to the given type",
157157
request: {
158158
body: {
159159
content: {
@@ -170,7 +170,7 @@ const updatePurchaseLineItemTypeRoute = createRoute({
170170
schema: UpdatePurchaseLineItemResponseSchema,
171171
},
172172
},
173-
description: "Successfully updated the line item's type",
173+
description: "Successfully updated the line items' type",
174174
},
175175
...openApiErrorCodes("Error modifying purchase line item"),
176176
},

backend/src/modules/purchase-line-item/controller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface IPurchaseLineItemController {
2626
updatePurchaseLineItemCategory(
2727
ctx: Context
2828
): ControllerResponse<TypedResponse<UpdatePurchaseLineItemResponse, 200>>;
29+
2930
updatePurchaseLineItemType(ctx: Context): ControllerResponse<TypedResponse<UpdatePurchaseLineItemResponse, 200>>;
3031
}
3132

@@ -85,7 +86,7 @@ export class PurchaseLineItemController implements IPurchaseLineItemController {
8586
const request = UpdatePurchaseLineItemCategoryDTOSchema.parse(json);
8687

8788
const updated = await this.purchaseLineItemService.updatePurchaseLineItemCategory(
88-
request.id,
89+
request.ids,
8990
request.category,
9091
request.removeCategory
9192
);
@@ -99,7 +100,7 @@ export class PurchaseLineItemController implements IPurchaseLineItemController {
99100
const json = await ctx.req.json();
100101
const request = UpdatePurchaseLineItemTypeDTOSchema.parse(json);
101102

102-
const updated = await this.purchaseLineItemService.updatePurchaseLineItemType(request.id, request.type);
103+
const updated = await this.purchaseLineItemService.updatePurchaseLineItemType(request.ids, request.type);
103104

104105
return ctx.json(updated, 200);
105106
}

backend/src/modules/purchase-line-item/service.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ export interface IPurchaseLineItemService {
1515
): Promise<CreateOrChangePurchaseLineItemsResponse>;
1616
getPurchaseLineItem(id: string): Promise<GetPurchaseLineItemResponse>;
1717
getPurchaseLineItemsForPurchase(parentPurchaseId: string): Promise<GetPurchaseLineItemsFromParentResponse>;
18-
updatePurchaseLineItemCategory(id: string, category: string, removeCategory: boolean): Promise<PurchaseLineItem>;
19-
updatePurchaseLineItemType(id: string, type: PurchaseLineItemType): Promise<PurchaseLineItem>;
18+
updatePurchaseLineItemCategory(
19+
ids: string[],
20+
category: string,
21+
removeCategory: boolean
22+
): Promise<PurchaseLineItem[]>;
23+
updatePurchaseLineItemType(ids: string[], type: PurchaseLineItemType): Promise<PurchaseLineItem[]>;
2024
}
2125

2226
export class PurchaseLineItemService implements IPurchaseLineItemService {
@@ -60,14 +64,14 @@ export class PurchaseLineItemService implements IPurchaseLineItemService {
6064
};
6165

6266
updatePurchaseLineItemCategory = withServiceErrorHandling(
63-
async (id: string, category: string, removeCategory: boolean): Promise<PurchaseLineItem> => {
64-
return this.purchaseLineItemTransaction.updatePurchaseLineItemCategory(id, category, removeCategory);
67+
async (ids: string[], category: string, removeCategory: boolean): Promise<PurchaseLineItem[]> => {
68+
return this.purchaseLineItemTransaction.updatePurchaseLineItemCategory(ids, category, removeCategory);
6569
}
6670
);
6771

6872
updatePurchaseLineItemType = withServiceErrorHandling(
69-
async (id: string, type: PurchaseLineItemType): Promise<PurchaseLineItem> => {
70-
return this.purchaseLineItemTransaction.updatePurchaseLineItemType(id, type);
73+
async (ids: string[], type: PurchaseLineItemType): Promise<PurchaseLineItem[]> => {
74+
return this.purchaseLineItemTransaction.updatePurchaseLineItemType(ids, type);
7175
}
7276
);
7377
}

backend/src/modules/purchase-line-item/transaction.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ export interface IPurchaseLineItemTransaction {
99
createOrUpdatePurchaseLineItems(payload: CreateOrChangePurchaseLineItemsDTO): Promise<PurchaseLineItem[]>;
1010
getPurchaseLineItem(id: string): Promise<PurchaseLineItem | null>;
1111
getPurchaseLineItemsForPurchase(purchaseId: string): Promise<PurchaseLineItem[]>;
12-
updatePurchaseLineItemCategory(id: string, category: string, removeCategory: boolean): Promise<PurchaseLineItem>;
13-
updatePurchaseLineItemType(id: string, type: PurchaseLineItemType): Promise<PurchaseLineItem>;
12+
updatePurchaseLineItemCategory(
13+
ids: string[],
14+
category: string,
15+
removeCategory: boolean
16+
): Promise<PurchaseLineItem[]>;
17+
updatePurchaseLineItemType(ids: string[], type: PurchaseLineItemType): Promise<PurchaseLineItem[]>;
1418
}
1519

1620
export class PurchaseLineItemTransaction implements IPurchaseLineItemTransaction {
@@ -68,39 +72,39 @@ export class PurchaseLineItemTransaction implements IPurchaseLineItemTransaction
6872
}
6973

7074
async updatePurchaseLineItemCategory(
71-
id: string,
75+
ids: string[],
7276
category: string,
7377
removeCategory: boolean
74-
): Promise<PurchaseLineItem> {
78+
): Promise<PurchaseLineItem[]> {
7579
const qb = this.db.createQueryBuilder().update(PurchaseLineItem);
7680

7781
if (removeCategory) {
78-
qb.set({ category: null }).where("id = :id AND category = :category", { id, category });
82+
qb.set({ category: null }).where("id IN (:...ids) AND category = :category", { ids, category });
7983
} else {
80-
qb.set({ category }).where("id = :id", { id });
84+
qb.set({ category }).where("id IN (:...ids)", { ids });
8185
}
8286

8387
const response = await qb.returning("*").execute();
8488

8589
if (!response) {
8690
throw Boom.notFound("Error updating the purchase line item category");
8791
}
88-
return response.raw[0];
92+
return response.raw;
8993
}
9094

91-
async updatePurchaseLineItemType(id: string, type: PurchaseLineItemType): Promise<PurchaseLineItem> {
95+
async updatePurchaseLineItemType(ids: string[], type: PurchaseLineItemType): Promise<PurchaseLineItem[]> {
9296
const response = await this.db
9397
.createQueryBuilder()
9498
.update(PurchaseLineItem)
9599
.set({ type: type })
96-
.where({ id })
100+
.where("id IN (:...ids)", { ids })
97101
.returning("*")
98102
.execute();
99103

100104
if (!response || response.affected === 0) {
101105
throw Boom.notFound("Error updating the purchase line item type");
102106
}
103107

104-
return response.raw[0];
108+
return response.raw;
105109
}
106110
}

backend/src/modules/purchase-line-item/types.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ export const CreateOrChangePurchaseLineItemsDTOSchema = z.object({
2828
});
2929

3030
export const UpdatePurchaseLineItemCategoryDTOSchema = z.object({
31-
id: z.uuid(),
31+
ids: z.array(z.uuid()),
3232
category: z.string().nonempty().max(LINE_ITEM_CATEGORY_CHARS),
3333
removeCategory: z.boolean(),
3434
});
3535

3636
export const UpdatePurchaseLineItemTypeDTOSchema = z.object({
37-
id: z.uuid(),
37+
ids: z.array(z.uuid()),
3838
type: z.enum(PurchaseLineItemType),
3939
});
4040

@@ -68,9 +68,11 @@ export const GetPurchaseLineItemResponseSchema = z.object({
6868

6969
export const GetPurchaseLineItemsFromParentResponseSchema = z.array(GetPurchaseLineItemResponseSchema);
7070

71-
export const UpdatePurchaseLineItemResponseSchema = GetPurchaseLineItemResponseSchema.extend({
72-
quickbooksDateCreated: z.iso.datetime().nullable().optional(),
73-
});
71+
export const UpdatePurchaseLineItemResponseSchema = z.array(
72+
GetPurchaseLineItemResponseSchema.extend({
73+
quickbooksDateCreated: z.iso.datetime().nullable().optional(),
74+
})
75+
);
7476

7577
//Controller Responses
7678
export type CreateOrChangePurchaseLineItemsResponse = z.infer<typeof CreateOrChangePurchaseLineItemsResponseSchema>;

backend/src/modules/quickbooks/service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export class QuickbooksService implements IQuickbooksService {
291291
for (const val of result) {
292292
if (val.type === null) {
293293
const type = await classifyLineItem(val);
294-
await this.purchaseLineItemTransaction.updatePurchaseLineItemType(val.id, type);
294+
await this.purchaseLineItemTransaction.updatePurchaseLineItemType([val.id], type);
295295
}
296296
}
297297

0 commit comments

Comments
 (0)