diff --git a/src/models/Categroy.ts b/src/models/Categroy.ts index 63626cc..7e1d7ce 100644 --- a/src/models/Categroy.ts +++ b/src/models/Categroy.ts @@ -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"; @@ -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) { diff --git a/test/issues/github/issue_60.e2e.ts b/test/issues/github/issue_60.e2e.ts new file mode 100644 index 0000000..2641cd8 --- /dev/null +++ b/test/issues/github/issue_60.e2e.ts @@ -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); + }); + + }); + +});