Skip to content

Commit

Permalink
Merge pull request #9 from Roger13579/features/P005
Browse files Browse the repository at this point in the history
fix: product property - soldAmount
  • Loading branch information
y0000ga authored May 7, 2024
2 parents fdab7bd + a4c65a2 commit e631e67
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build": "tsc",
"start": "node src/dist/bin/www.js",
"dev": "tsc && node src/dist/bin/www.js",
"swagger": "node src/dist/swagger.js"
"swagger": "node src/dist/swagger/index.js"
},
"keywords": [],
"author": "",
Expand Down
5 changes: 4 additions & 1 deletion src/controller/productController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ResponseObject } from '../utils/responseObject';
import { ProductService } from '../service/productService';
import { NewProductDto } from '../dto/newProductDto';
import { NewProductVo } from '../vo/newProductVo';
import { TCreateProductsReq } from '../types/product.type';

class ProductController extends BaseController {
private readonly productService = new ProductService();
Expand All @@ -18,7 +19,9 @@ class ProductController extends BaseController {
);
};

public createProducts = async (req: Request): Promise<ResponseObject> => {
public createProducts = async (
req: TCreateProductsReq,
): Promise<ResponseObject> => {
const newProductsDto = new NewProductDto(req);
const products = await this.productService.createProducts(newProductsDto);
return this.formatResponse(
Expand Down
13 changes: 8 additions & 5 deletions src/dto/newProductDto.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Request } from 'express';
import { IProduct } from '../models/product';
import { TCreateProductsReq } from '../types/product.type';

export class NewProductDto {
private readonly products: [IProduct];
private readonly products: IProduct[];

get getNewProducts(): [IProduct] {
get getNewProducts(): IProduct[] {
return this.products;
}

constructor(req: Request) {
this.products = req.body.products;
constructor(req: TCreateProductsReq) {
this.products = req.body.products.map((product) => ({
...product,
soldAmount: 0,
}));
}
}
7 changes: 6 additions & 1 deletion src/models/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface IProduct extends Document, ITimestamp {
cancelPolicies?: [string];
certificates?: [string];
comments?: [string];
soldAmount: number;
}

const schema = new Schema<IProduct>(
Expand Down Expand Up @@ -79,6 +80,10 @@ const schema = new Schema<IProduct>(
message: '總數量錯誤,請確認 plans 內部數量',
},
},
soldAmount: {
type: Number,
default: 0,
},
plans: {
type: [
{
Expand Down Expand Up @@ -122,7 +127,7 @@ const schema = new Schema<IProduct>(
required: true,
validate: {
validator: function (sellStartAt: Date) {
return moment().isBefore(sellStartAt, 'day');
return moment().isBefore(moment(sellStartAt), 'day');
},
message: '販賣開始時間必須晚於現在時間至少一天',
},
Expand Down
160 changes: 120 additions & 40 deletions src/swagger-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,12 @@
"type": "object",
"properties": {}
}
}
},
"required": [
"status",
"message",
"data"
]
},
"Error6202": {
"type": "object",
Expand All @@ -350,52 +355,63 @@
"message": {
"type": "string",
"example": "密碼錯誤"
},
"data": {
"type": "object",
"properties": {}
}
}
},
"required": [
"status",
"message"
]
},
"ErrorToken": {
"type": "object",
"properties": {
"status": {
"type": "string",
"example": "false"
"example": "6303"
},
"message": {
"type": "string",
"example": "你尚未登入"
},
"error": {
"type": "object",
"properties": {
"name": {
"type": "string",
"example": "40300"
}
}
"example": "尚未登入"
}
}
},
"required": [
"status",
"message"
]
},
"Error404": {
"type": "object",
"properties": {
"status": {
"type": "number",
"example": 404
},
"message": {
"type": "string",
"example": "找不到頁面"
}
}
},
"required": [
"status",
"message"
]
},
"Error500": {
"type": "object",
"properties": {
"status": {
"type": "number",
"example": 500
},
"message": {
"type": "string",
"example": "系統錯誤,請稍後再試"
}
}
},
"required": [
"status",
"message"
]
},
"SignUpForm": {
"type": "object",
Expand Down Expand Up @@ -489,7 +505,12 @@
}
}
}
}
},
"required": [
"status",
"message",
"data"
]
},
"UserDetail": {
"type": "object",
Expand Down Expand Up @@ -536,7 +557,12 @@
}
}
}
}
},
"required": [
"status",
"message",
"data"
]
},
"UpdateUserDetail": {
"type": "object",
Expand All @@ -553,40 +579,65 @@
"type": "object",
"properties": {}
}
}
},
"required": [
"status",
"message",
"data"
]
},
"RegisterEmailSuccess": {
"type": "object",
"properties": {
"status": {
"type": "string",
"example": "success"
"example": "6000"
},
"message": {
"type": "string",
"example": "請至信箱確認是否收到驗證信"
}
}
},
"required": [
"status",
"message"
]
},
"RegisterEmailError": {
"type": "object",
"properties": {
"status": {
"type": "string",
"example": "6504"
},
"message": {
"type": "string",
"example": "請稍後重試或聯絡管理員"
}
}
},
"required": [
"status",
"message"
]
},
"ValidateEmailError": {
"type": "object",
"properties": {
"status": {
"type": "string",
"example": "6505"
},
"message": {
"type": "string",
"example": "信箱驗證失敗"
}
}
},
"required": [
"status",
"message"
]
},
"CustomCreateProductsSuccess": {
"CreateProductsSuccess": {
"type": "object",
"properties": {
"status": {
Expand Down Expand Up @@ -633,6 +684,10 @@
"type": "number",
"example": 100
},
"soldAmount": {
"type": "number",
"example": 0
},
"plans": {
"type": "array",
"items": {
Expand Down Expand Up @@ -671,19 +726,19 @@
},
"sellEndAt": {
"type": "string",
"example": "2024-05-09T12:19:42.178Z"
"example": "2024-05-09T15:42:53.661Z"
},
"sellStartAt": {
"type": "string",
"example": "2024-05-09T10:19:42.180Z"
"example": "2024-05-09T13:42:53.662Z"
},
"endAt": {
"type": "string",
"example": "2024-05-09T16:19:42.180Z"
"example": "2024-05-09T19:42:53.662Z"
},
"startAt": {
"type": "string",
"example": "2024-05-09T14:19:42.180Z"
"example": "2024-05-09T17:42:53.662Z"
},
"tags": {
"type": "array",
Expand Down Expand Up @@ -768,6 +823,7 @@
"theater",
"price",
"amount",
"soldAmount",
"introduction",
"isLaunched",
"isPublic",
Expand All @@ -788,6 +844,23 @@
"data"
]
},
"CreateProductsError": {
"type": "object",
"properties": {
"status": {
"type": "string",
"example": "6213"
},
"message": {
"type": "string",
"example": "新增錯誤"
}
},
"required": [
"status",
"message"
]
},
"CustomCreateProductsObj": {
"type": "object",
"required": [
Expand All @@ -806,6 +879,7 @@
"theater",
"price",
"amount",
"soldAmount",
"startAt",
"endAt",
"sellStartAt",
Expand Down Expand Up @@ -881,6 +955,12 @@
"min": 0,
"description": "票券總量,不得低於最大人數方案。"
},
"soldAmount": {
"type": "number",
"example": 0,
"min": 0,
"description": "已銷售數量"
},
"plans": {
"type": "array",
"items": {
Expand Down Expand Up @@ -937,26 +1017,26 @@
},
"sellEndAt": {
"type": "Date",
"example": "2024-05-09T12:19:42.178Z",
"min": "2024-05-08T11:19:42.180Z",
"example": "2024-05-09T15:42:53.661Z",
"min": "2024-05-08T14:42:53.662Z",
"description": "販賣結束時間,必須晚於販賣開始時間至少一個小時"
},
"sellStartAt": {
"type": "Date",
"example": "2024-05-09T10:19:42.180Z",
"min": "2024-05-08T10:19:42.180Z",
"example": "2024-05-09T13:42:53.662Z",
"min": "2024-05-08T13:42:53.662Z",
"description": "販賣開始時間,必須晚於現在時間至少一天"
},
"endAt": {
"type": "Date",
"example": "2024-05-09T16:19:42.180Z",
"min": "2024-05-08T13:19:42.180Z",
"example": "2024-05-09T19:42:53.662Z",
"min": "2024-05-08T16:42:53.662Z",
"description": "活動結束時間,必須晚於活動開始時間至少一個小時"
},
"startAt": {
"type": "Date",
"example": "2024-05-09T14:19:42.180Z",
"min": "2024-05-08T12:19:42.180Z",
"example": "2024-05-09T17:42:53.662Z",
"min": "2024-05-08T15:42:53.663Z",
"description": "活動開始時間,必須晚於販售結束時間至少一個小時"
},
"tags": {
Expand Down
Loading

0 comments on commit e631e67

Please sign in to comment.