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
14 changes: 13 additions & 1 deletion src/models/Categroy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { model, SchemaDefinition, Model as M, SchemaTypes } from "mongoose";
import { Base, IDoc, IDocRaw, ObjectId, MODIFY_MOTHODS } from "@models/common";
import { Base, IDoc, IDocRaw, ObjectId, MODIFY_MOTHODS, existsValidator } from "@models/common";
import { IValues, Flag as ValueFlag } from "@models/Value";
import { DEF_PER_COUNT } from "@dtos/page";
import { isArray } from "util";
Expand Down Expand Up @@ -40,6 +40,18 @@ export interface ICategoryRaw extends ICategory {

const CategorySchema = new Base(Definition).createSchema();

// region Validators
CategorySchema.path("name").validate({
isAsync: true,
validator: async function nameExistValidator(value, respond) {
return respond(await existsValidator.bind(this)(
Model, "name", value
));
},
message: "The name is exist"
});
// endregion Validators

const getIdGroups = (obj): string[] => {
const selfIdArr = [ obj._id.toString() ];
if (obj.pid) {
Expand Down
47 changes: 47 additions & 0 deletions test/issues/github/issue_60.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import faker = require("faker");

import {
connect, drop, addCategoryAndRegexp
} from "../../helpers/database";
import { init } from "../../helpers/server";
import { newIds, newName } from "../../helpers/utils";
import { GuestRequest, AdminRequest } from "../../helpers/request";
import * as files from "../../helpers/files";

/**
* Fix [Issue 60](https://github.com/BoxSystem/StoreBox-Api/issues/60)
*/
describe("Fix Issues", () => {

let request: AdminRequest;

before(() => {
return connect();
});

const ids = newIds();

after(() => {
return drop(ids);
});

before("login", async () => {
request = await new GuestRequest(await init(), ids).login();
});

describe("Github 60 [Can Add Category with exist name]", () => {

it("Cant", async () => {
const name = newName();
const req1 =
await request.post("/api/v1/categories").send({ name }).then();
req1.status.should.be.eql(201);
ids.categories.push(req1.body._id);
const req2 =
await request.post("/api/v1/categories").send({ name }).then();
req2.status.should.be.eql(400);
});

});

});