diff --git a/prisma/migrations/20240613084622_add_custom_attributes_json/migration.sql b/prisma/migrations/20240613084622_add_custom_attributes_json/migration.sql new file mode 100644 index 0000000..cadf124 --- /dev/null +++ b/prisma/migrations/20240613084622_add_custom_attributes_json/migration.sql @@ -0,0 +1,14 @@ +/* + Warnings: + + - You are about to drop the column `attributes` on the `Product` table. All the data in the column will be lost. + +*/ +-- AlterTable +ALTER TABLE "Product" DROP COLUMN "attributes"; + +-- AlterTable +ALTER TABLE "Service" ADD COLUMN "attributes" JSONB; + +-- AlterTable +ALTER TABLE "Work" ADD COLUMN "attributes" JSONB; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7a695e3..20a2c4e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -537,9 +537,10 @@ model EmployeeOtherInfo { model Service { id String @id @default(uuid()) - code String - name String - detail String + code String + name String + detail String + attributes Json? status Status @default(CREATED) @@ -554,7 +555,8 @@ model Service { model Work { id String @id @default(uuid()) - name String + name String + attributes Json? status Status @default(CREATED) @@ -655,8 +657,6 @@ model Product { workProduct WorkProduct[] - attributes Json? - createdBy String? createdAt DateTime @default(now()) updateBy String? diff --git a/src/controllers/product/product-controller.ts b/src/controllers/product/product-controller.ts index 8b8fbbe..59e550e 100644 --- a/src/controllers/product/product-controller.ts +++ b/src/controllers/product/product-controller.ts @@ -10,7 +10,7 @@ import { Route, Security, Tags, - Queries, + Query, } from "tsoa"; import { Prisma, Status } from "@prisma/client"; @@ -34,9 +34,6 @@ type ProductCreate = { price: number; agentPrice: number; serviceCharge: number; - attributes?: { - [key: string]: Date | string | number | null; - }; }; type ProductUpdate = { @@ -46,9 +43,6 @@ type ProductUpdate = { price: number; agentPrice: number; serviceCharge: number; - attributes?: { - [key: string]: Date | string | number | null; - }; }; function imageLocation(id: string) { @@ -61,55 +55,12 @@ function imageLocation(id: string) { export class ProductController extends Controller { @Get() async getProduct( - @Queries() - qs: { - query?: string; - page?: number; - pageSize?: number; - filter?: string[]; - }, + @Query() query: string = "", + @Query() page: number = 1, + @Query() pageSize: number = 30, ) { - qs.page = qs.page ?? 1; - qs.pageSize = qs.pageSize ?? 30; - qs.query = qs.query ?? ""; - - const { query, page, pageSize, filter } = qs; - const where = { OR: [{ name: { contains: query } }, { detail: { contains: query } }], - AND: filter - ?.map((v) => { - const match = /[\w\-]+:[\W\w]+$/.exec(v); - - if (!match) return []; - - const [key, ...rest] = v.split(":"); - const val = rest.join(); - - return { - OR: [ - { - attributes: { - path: [key], - string_contains: val, - }, - }, - { - attributes: { - path: [key], - equals: Number.isFinite(+val) - ? +val - : val === "true" - ? true - : val === "false" - ? false - : val, - }, - }, - ], - }; - }) - .flat(), } satisfies Prisma.ProductWhereInput; const [result, total] = await prisma.$transaction([ @@ -172,7 +123,6 @@ export class ProductController extends Controller { data: { ...body, code: `${body.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`, - attributes: body.attributes, createdBy: req.user.name, updateBy: req.user.name, }, diff --git a/src/controllers/service/service-controller.ts b/src/controllers/service/service-controller.ts index ff8f49c..e5ac894 100644 --- a/src/controllers/service/service-controller.ts +++ b/src/controllers/service/service-controller.ts @@ -30,6 +30,9 @@ type ServiceCreate = { code: "MOU" | "mou"; name: string; detail: string; + attributes?: { + [key: string]: any; + }; workId: string[]; }; @@ -37,6 +40,9 @@ type ServiceUpdate = { name: string; detail: string; workId: string[]; + attributes?: { + [key: string]: any; + }; }; function imageLocation(id: string) { diff --git a/src/controllers/work/work-controller.ts b/src/controllers/work/work-controller.ts index 8702cf8..8bd491f 100644 --- a/src/controllers/work/work-controller.ts +++ b/src/controllers/work/work-controller.ts @@ -22,11 +22,17 @@ import HttpStatus from "../../interfaces/http-status"; type WorkCreate = { name: string; productId: string[]; + attributes?: { + [key: string]: any; + }; }; type WorkUpdate = { name?: string; productId?: string[]; + attributes?: { + [key: string]: any; + }; }; @Route("api/v1/work")