Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "storebox-api",
"version": "1.3.1",
"version": "1.3.2",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/common/dtos/collections.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
3 changes: 2 additions & 1 deletion src/modules/common/pipes/parse-int.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { HttpException } from "@nestjs/common";
import {
PipeTransform, Pipe, ArgumentMetadata, HttpStatus
} from "@nestjs/common";
import { isString } from "util";

@Pipe()
export class ParseIntPipe implements PipeTransform<string> {
public async transform(value: object | string, metadata: ArgumentMetadata) {
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);
Expand Down
3 changes: 2 additions & 1 deletion src/modules/public/goods/goods.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand Down
14 changes: 12 additions & 2 deletions test/api/collections/collections.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
});

});
3 changes: 3 additions & 0 deletions test/helpers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down