diff --git a/package.json b/package.json index 7746b9b..43a3d80 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "storebox-api", - "version": "1.3.1", + "version": "1.3.2", "description": "", "main": "index.js", "scripts": { diff --git a/src/modules/common/dtos/collections.dto.ts b/src/modules/common/dtos/collections.dto.ts index cc9f4c1..f64e692 100644 --- a/src/modules/common/dtos/collections.dto.ts +++ b/src/modules/common/dtos/collections.dto.ts @@ -10,7 +10,7 @@ export interface ICollection { creator?: ObjectId; } -const NAME_REGEXP = /^[\da-z]\w{2,}[\da-z]$/i; +const NAME_REGEXP = /^[\da-z]\w{2}[\w ]*[\da-z]$/i; export class GetCollectionNameDto { @ApiModelProperty({ diff --git a/src/modules/common/pipes/parse-int.pipe.ts b/src/modules/common/pipes/parse-int.pipe.ts index 3984d24..c5afcc5 100644 --- a/src/modules/common/pipes/parse-int.pipe.ts +++ b/src/modules/common/pipes/parse-int.pipe.ts @@ -2,6 +2,7 @@ import { HttpException } from "@nestjs/common"; import { PipeTransform, Pipe, ArgumentMetadata, HttpStatus } from "@nestjs/common"; +import { isString } from "util"; @Pipe() export class ParseIntPipe implements PipeTransform { @@ -9,7 +10,7 @@ export class ParseIntPipe implements PipeTransform { if (typeof(value) === "object") { for (const key of Object.keys(value)) { const v = value[key]; - if (!v || /\D/.test(v)) { + if (!v || !isString(v) || /\D/.test(v)) { continue; } value[key] = parseInt(v, 10); diff --git a/src/modules/public/goods/goods.controller.ts b/src/modules/public/goods/goods.controller.ts index cb4afb2..ea13deb 100644 --- a/src/modules/public/goods/goods.controller.ts +++ b/src/modules/public/goods/goods.controller.ts @@ -10,6 +10,7 @@ import { GoodsService } from "@services/goods"; import { CategoriesService } from "@services/categories"; import { Roles } from "@decorators/roles"; import { ToArrayPipe } from "@pipes/to-array"; +import { ParseIntPipe } from "@pipes/parse-int"; import { ListResponse, DEF_PER_COUNT } from "@dtos/page"; import { LogsService } from "@services/logs"; import { reduce } from "lodash"; @@ -35,7 +36,7 @@ export class GoodsController { @ApiResponse({ status: HttpStatus.OK, type: ListResponse }) // endregion Swagger Docs public async getList( - @Query(new ToArrayPipe("tags")) query: GoodsQueryDto + @Query(new ToArrayPipe("tags"), new ParseIntPipe()) query: GoodsQueryDto ) { const categoryModels = await this.categoriesSvr.getObjectsByTags(query.tags); diff --git a/test/api/collections/collections.e2e.ts b/test/api/collections/collections.e2e.ts index 43dd9ac..fb81297 100644 --- a/test/api/collections/collections.e2e.ts +++ b/test/api/collections/collections.e2e.ts @@ -173,7 +173,6 @@ describe("Collections E2E Api", () => { }); step("Add New Collection With Name Field", async () => { - await sleep(50); const goodIds = goods.filter((_, i) => i === 0) .reduce((arr, cur) => { arr.push(cur._id); @@ -190,7 +189,6 @@ describe("Collections E2E Api", () => { }); step("Fail to Add New Collection With Same Name Field", async () => { - await sleep(50); const goodIds = goods.filter((_, i) => i === 0) .reduce((arr, cur) => { arr.push(cur._id); @@ -301,4 +299,16 @@ describe("Collections E2E Api", () => { result.total.should.eql(2); }); + step("Display in public url", async () => { + const id = ids.collections[ids.collections.length - 1]; + const { body: result } = + await request.get(`/api/v1/collections/${id}`).then(); + const { status, body } = + await request.get(`/collections/${result.name}`).then(); + status.should.be.eql(200); + body.should.have.property("name", result.name); + body.should.have.property("creator").which.is.a.String(); + body.goods.should.have.property("total", result.goods.total); + }); + }); diff --git a/test/helpers/files.ts b/test/helpers/files.ts index e7b4822..41ede23 100644 --- a/test/helpers/files.ts +++ b/test/helpers/files.ts @@ -71,6 +71,9 @@ export const newFile = async (filename = newFilename()) => { fs.mkdirpSync(folderpath); } const filepath = `${folderpath}/${filename}`; + if (fs.existsSync(filepath)) { + return newFile(); + } fs.writeFileSync(filepath, JSON.stringify({ data: Math.random() }), { encoding: "utf-8" }); diff --git a/test/helpers/request.ts b/test/helpers/request.ts index ac9694c..c4e9975 100644 --- a/test/helpers/request.ts +++ b/test/helpers/request.ts @@ -236,12 +236,13 @@ class LoginedRequest extends BaseRequest { export class AdminRequest extends LoginedRequest { public async addCollection(goods: ObjectId[], name = newName()) { + goods = goods.map((item) => item.toString()); const { status, body } = await this.post("/api/v1/collections") .send({ name, goods }).then(); - status.should.be.eql(201); if (status === 201) { this.ids.collections.push(body._id); } + status.should.be.eql(201); await sleep(200); return this; }