Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jakir-coder committed May 19, 2024
1 parent c85398d commit 8686863
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 22 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@iam-hussain/qd-copilot": "^1.1.18",
"@iam-hussain/qd-copilot": "^1.2.1",
"@prisma/client": "^5.12.1",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
Expand Down
9 changes: 9 additions & 0 deletions prisma/migrations/20240519092909_prepare_at/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
Warnings:
- You are about to drop the column `prepared` on the `Item` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "Item" DROP COLUMN "prepared",
ADD COLUMN "preparedAt" TIMESTAMP(3);
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ model Item {
placeAt DateTime @default(now())
placedAt DateTime?
acceptedAt DateTime?
prepared DateTime?
preparedAt DateTime?
status ITEM_STATUS @default(PLACED)
product Product @relation(fields: [productId], references: [id])
productId String
Expand Down
28 changes: 28 additions & 0 deletions src/api/item/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ItemUpdateReqSchema } from '@iam-hussain/qd-copilot';
import express, { Request, Response, Router } from 'express';

import { handleServiceResponse, validateRequest } from '@/utils/http-handlers';
import { validateAccess } from '@/utils/shield-handler';

import { itemService } from './service';

export const itemRouter: Router = (() => {
const router = express.Router();

router.post(
'/order/item/:id',
validateAccess('SIGN_STORE'),
validateRequest(ItemUpdateReqSchema),
async (req: Request, res: Response) => {
const serviceResponse = await itemService.update(
req.context.store.slug,
req.params.id,
req.body,
req.context.user.id
);
handleServiceResponse(serviceResponse, res);
}
);

return router;
})();
3 changes: 1 addition & 2 deletions src/api/item/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const itemRepository = {
},
});
},
update: async (slug: string, id: string, orderId: string, data: any, userId: string) => {
update: async (slug: string, id: string, orderId: string, data: any) => {
return database.item.update({
where: {
id,
Expand All @@ -29,7 +29,6 @@ export const itemRepository = {
},
data: {
...data,
updatedBy: userId,
},
});
},
Expand Down
5 changes: 3 additions & 2 deletions src/api/item/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export const itemService = {
if (!data?.orderId) {
return {};
}
const input = itemTransformer.getUpdateItemData(data);
const repositoryResponse = await itemRepository.update(slug, id, data.orderId, input, userId);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const input = itemTransformer.getUpdateItemData(data, userId);
const repositoryResponse = await itemRepository.update(slug, id, data.orderId, input);
return repositoryResponse;
},
};
33 changes: 21 additions & 12 deletions src/api/item/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getGroupedItems, ItemCreateSchemaType, ItemUpdateSchemaType } from '@iam-hussain/qd-copilot';
import { Item, PRODUCT_TYPE } from '@prisma/client';
import { Item, ITEM_STATUS, PRODUCT_TYPE } from '@prisma/client';
import _ from 'lodash';

import dateTime from '@/libs/date-time';
Expand Down Expand Up @@ -56,15 +56,19 @@ const getItemTypeDivided = (input: Item[]) => {
items: nonDraft,
drafted,
summary: getGroupedItems(nonDraft),
scheduled: nonDraft.filter((e) => dateTime.isAfterDate(e.placeAt)),
placed: nonDraft.filter((e) => e.placedAt && dateTime.isBeforeDate(e.placedAt)),
accepted: nonDraft.filter((e) => e.acceptedAt && dateTime.isBeforeDate(e.acceptedAt)),
prepared: nonDraft.filter((e) => e.prepared && dateTime.isBeforeDate(e.prepared)),
scheduled: nonDraft.filter((e) => e.status === ITEM_STATUS.SCHEDULED && dateTime.isAfterDate(e.placeAt)),
placed: nonDraft.filter((e) => e.status === ITEM_STATUS.PLACED && e.placedAt && dateTime.isBeforeDate(e.placedAt)),
accepted: nonDraft.filter(
(e) => e.status === ITEM_STATUS.ACCEPTED && e.acceptedAt && dateTime.isBeforeDate(e.acceptedAt)
),
prepared: nonDraft.filter(
(e) => e.status === ITEM_STATUS.PREPARED && e.preparedAt && dateTime.isBeforeDate(e.preparedAt)
),
};
};

const getUpdateItemData = (data: ItemUpdateSchemaType) => {
return _.pick(data, [
const getUpdateItemData = (data: ItemUpdateSchemaType, userId: string) => {
const output = _.pick(data, [
'id',
'title',
'note',
Expand All @@ -76,14 +80,19 @@ const getUpdateItemData = (data: ItemUpdateSchemaType) => {
'placeAt',
'placedAt',
'acceptedAt',
'prepared',
'preparedAt',
'status',
'productId',
'orderId',
'createdId',
'updatedId',
'billId',
]);

return {
...output,
updatedBy: {
connect: {
id: userId,
},
},
};
};

export const itemTransformer = {
Expand Down
2 changes: 2 additions & 0 deletions src/providers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { authRouter } from '@/api/authentication';
import { authTransformer } from '@/api/authentication/transfomer';
import { categoryRouter } from '@/api/category';
import { healthCheckRouter } from '@/api/health-check';
import { itemRouter } from '@/api/item';
import { orderRouter } from '@/api/order';
import { productRouter } from '@/api/product';
import { storeRouter } from '@/api/store';
Expand Down Expand Up @@ -76,6 +77,7 @@ app.use('/api/store', storeRouter);
app.use('/api/store', productRouter);
app.use('/api/store', categoryRouter);
app.use('/api/store', orderRouter);
app.use('/api/store', itemRouter);
app.use('/api/user', userRouter);

// Error handlers
Expand Down

0 comments on commit 8686863

Please sign in to comment.