diff --git a/src/__tests__/boardDb.test.ts b/src/__tests__/boardDb.test.ts index 06eaae1ab..57a0f5292 100644 --- a/src/__tests__/boardDb.test.ts +++ b/src/__tests__/boardDb.test.ts @@ -218,14 +218,6 @@ describe('Test board model', () => { expect(updatedStage.name).toEqual(stageName); }); - test('Change stage', async () => { - const pipelineToUpdate = await pipelineFactory({}); - const changedStage = await Stages.changeStage(stage._id, pipelineToUpdate._id); - - expect(changedStage).toBeDefined(); - expect(changedStage.pipelineId).toEqual(pipelineToUpdate._id); - }); - test('Update stage orders', async () => { const stageToOrder = await stageFactory({}); @@ -237,34 +229,4 @@ describe('Test board model', () => { expect(updatedStage.order).toBe(5); expect(updatedStageToOrder.order).toBe(9); }); - - test('Remove stage', async () => { - await Deals.updateMany({}, { $set: { stageId: 'stageId' } }); - - const isDeleted = await Stages.removeStage(stage.id); - - expect(isDeleted).toBeTruthy(); - }); - - test('Remove stage not found', async () => { - expect.assertions(1); - - const fakeStageId = 'fakeStageId'; - - try { - await Stages.removeStage(fakeStageId); - } catch (e) { - expect(e.message).toEqual('Stage not found'); - } - }); - - test("Can't remove a stage", async () => { - expect.assertions(1); - - try { - await Stages.removeStage(stage._id); - } catch (e) { - expect(e.message).toEqual("Can't remove a stage"); - } - }); }); diff --git a/src/db/models/Boards.ts b/src/db/models/Boards.ts index 630ae5235..5ec63e92d 100644 --- a/src/db/models/Boards.ts +++ b/src/db/models/Boards.ts @@ -12,7 +12,6 @@ import { pipelineSchema, stageSchema, } from './definitions/boards'; -import { BOARD_TYPES } from './definitions/constants'; export interface IOrderInput { _id: string; @@ -22,7 +21,7 @@ export interface IOrderInput { // Not mongoose document, just stage shaped plain object type IPipelineStage = IStage & { _id: string }; -const createOrUpdatePipelineStages = async (stages: IPipelineStage[], pipelineId: string) => { +const createOrUpdatePipelineStages = async (stages: IPipelineStage[], pipelineId: string, type: string) => { let order = 0; const validStageIds: string[] = []; @@ -67,10 +66,27 @@ const createOrUpdatePipelineStages = async (stages: IPipelineStage[], pipelineId validStageIds.push(createdStage._id); } } + if (bulkOpsPrevEntry.length > 0) { await Stages.bulkWrite(bulkOpsPrevEntry); } + const ITEMS = { + deal: Deals, + ticket: Tickets, + task: Tasks, + }; + + const remainedStages = await Stages.find({ pipelineId, _id: { $nin: prevItemIds } }); + + for (const stage of remainedStages) { + const itemCount = await ITEMS[type].find({ stageId: stage._id }).countDocuments(); + + if (itemCount > 0) { + throw new Error('There is a stage that has a item'); + } + } + return Stages.deleteMany({ pipelineId, _id: { $nin: validStageIds } }); }; @@ -154,7 +170,7 @@ export const loadPipelineClass = () => { const pipeline = await Pipelines.create(doc); if (stages) { - await createOrUpdatePipelineStages(stages, pipeline._id); + await createOrUpdatePipelineStages(stages, pipeline._id, pipeline.type); } return pipeline; @@ -165,7 +181,7 @@ export const loadPipelineClass = () => { */ public static async updatePipeline(_id: string, doc: IPipeline, stages: IPipelineStage[]) { if (stages) { - await createOrUpdatePipelineStages(stages, _id); + await createOrUpdatePipelineStages(stages, _id, doc.type); } await Pipelines.updateOne({ _id }, { $set: doc }); @@ -213,9 +229,7 @@ export interface IStageModel extends Model { getStage(_id: string): Promise; createStage(doc: IStage): Promise; updateStage(_id: string, doc: IStage): Promise; - changeStage(_id: string, pipelineId: string): Promise; updateOrder(orders: IOrderInput[]): Promise; - removeStage(_id: string): void; } export const loadStageClass = () => { @@ -249,76 +263,12 @@ export const loadStageClass = () => { return Stages.findOne({ _id }); } - /** - * Change Stage - */ - public static async changeStage(_id: string, pipelineId: string) { - const stage = await Stages.updateOne({ _id }, { $set: { pipelineId } }); - - switch (stage.type) { - case BOARD_TYPES.DEAL: { - await Deals.updateMany({ stageId: _id }, { $set: { pipelineId } }); - - break; - } - case BOARD_TYPES.TICKET: { - await Tickets.updateMany({ stageId: _id }, { $set: { pipelineId } }); - - break; - } - case BOARD_TYPES.TASK: { - await Tasks.updateMany({ stageId: _id }, { $set: { pipelineId } }); - - break; - } - } - - return Stages.findOne({ _id }); - } - /* * Update given stages orders */ public static async updateOrder(orders: IOrderInput[]) { return updateOrder(Stages, orders); } - - /** - * Remove Stage - */ - public static async removeStage(_id: string) { - const stage = await Stages.findOne({ _id }); - - if (!stage) { - throw new Error('Stage not found'); - } - - let count; - - switch (stage.type) { - case BOARD_TYPES.DEAL: { - count = await Deals.find({ stageId: _id }).countDocuments(); - - break; - } - case BOARD_TYPES.TICKET: { - count = await Tickets.find({ stageId: _id }).countDocuments(); - - break; - } - case BOARD_TYPES.TASK: { - count = await Tasks.find({ stageId: _id }).countDocuments(); - - break; - } - } - - if (count > 0) { - throw new Error("Can't remove a stage"); - } - - return Stages.deleteOne({ _id }); - } } stageSchema.loadClass(Stage);