Skip to content
This repository has been archived by the owner on Nov 21, 2020. It is now read-only.

Commit

Permalink
fix(board): show a warning message if stage has a item && remove unus…
Browse files Browse the repository at this point in the history
…ed functions

Closes erxes/erxes#1205
  • Loading branch information
munkhjin0223 committed Aug 21, 2019
1 parent c783608 commit 1ed1727
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 108 deletions.
38 changes: 0 additions & 38 deletions src/__tests__/boardDb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({});

Expand All @@ -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");
}
});
});
90 changes: 20 additions & 70 deletions src/db/models/Boards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
pipelineSchema,
stageSchema,
} from './definitions/boards';
import { BOARD_TYPES } from './definitions/constants';

export interface IOrderInput {
_id: string;
Expand All @@ -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[] = [];
Expand Down Expand Up @@ -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 } });
};

Expand Down Expand Up @@ -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;
Expand All @@ -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 });
Expand Down Expand Up @@ -213,9 +229,7 @@ export interface IStageModel extends Model<IStageDocument> {
getStage(_id: string): Promise<IStageDocument>;
createStage(doc: IStage): Promise<IStageDocument>;
updateStage(_id: string, doc: IStage): Promise<IStageDocument>;
changeStage(_id: string, pipelineId: string): Promise<IStageDocument>;
updateOrder(orders: IOrderInput[]): Promise<IStageDocument[]>;
removeStage(_id: string): void;
}

export const loadStageClass = () => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1ed1727

Please sign in to comment.