From ed239cc846209ac41ce1d21246afe2f5d978b3b7 Mon Sep 17 00:00:00 2001 From: Khangarid Bayarsaikhan <32699094+khangaridb@users.noreply.github.com> Date: Fri, 30 Aug 2019 18:58:42 +0800 Subject: [PATCH] feat(notification): show stage names on notifications close #1124 --- src/data/resolvers/boardUtils.ts | 20 +++++++++++--------- src/data/resolvers/mutations/deals.ts | 2 +- src/data/resolvers/mutations/tasks.ts | 6 ++++-- src/data/resolvers/mutations/tickets.ts | 6 ++++-- src/db/models/Boards.ts | 14 ++++++++++++++ 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/data/resolvers/boardUtils.ts b/src/data/resolvers/boardUtils.ts index d6a671a22..fbbb4ad87 100644 --- a/src/data/resolvers/boardUtils.ts +++ b/src/data/resolvers/boardUtils.ts @@ -120,23 +120,25 @@ export const sendNotifications = async ({ }); }; -export const itemsChange = async (collection: any, item: any, type: string, destinationStageId: string) => { - const oldItem = await collection.findOne({ _id: item._id }); - const oldStageId = oldItem ? oldItem.stageId || '' : ''; +export const itemsChange = async (item: any, type: string, destinationStageId: string) => { + const oldStageId = item ? item.stageId || '' : ''; let action = `changed order of your ${type}:`; let content = `'${item.name}'`; if (oldStageId !== destinationStageId) { - const stage = await Stages.findOne({ _id: destinationStageId }); + const stage = await Stages.getStage(destinationStageId); + const oldStage = await Stages.getStage(oldStageId); - if (!stage) { - throw new Error('Stage not found'); - } + const pipeline = await Pipelines.getPipeline(stage.pipelineId || ''); + const oldPipeline = await Pipelines.getPipeline(oldStage.pipelineId || ''); - action = `moved your`; + const board = await Boards.getBoard(pipeline.boardId || ''); + const oldBoard = await Boards.getBoard(oldPipeline.boardId || ''); - content = `${type} '${item.name}' to the '${stage.name}'.`; + action = `moved '${item.name}' from ${oldBoard.name}-${oldPipeline.name}-${oldStage.name} to `; + + content = `${board.name}-${pipeline.name}-${stage.name}`; } return { content, action }; diff --git a/src/data/resolvers/mutations/deals.ts b/src/data/resolvers/mutations/deals.ts index 726b5ef56..c29ec6751 100644 --- a/src/data/resolvers/mutations/deals.ts +++ b/src/data/resolvers/mutations/deals.ts @@ -114,7 +114,7 @@ const dealMutations = { stageId: destinationStageId, }); - const { content, action } = await itemsChange(Deals, deal, 'deal', destinationStageId); + const { content, action } = await itemsChange(deal, 'deal', destinationStageId); await sendNotifications({ item: deal, diff --git a/src/data/resolvers/mutations/tasks.ts b/src/data/resolvers/mutations/tasks.ts index 13931325f..4783845e7 100644 --- a/src/data/resolvers/mutations/tasks.ts +++ b/src/data/resolvers/mutations/tasks.ts @@ -76,13 +76,15 @@ const taskMutations = { { _id, destinationStageId }: { _id: string; destinationStageId: string }, { user }: IContext, ) { - const task = await Tasks.updateTask(_id, { + const task = await Tasks.getTask(_id); + + await Tasks.updateTask(_id, { modifiedAt: new Date(), modifiedBy: user._id, stageId: destinationStageId, }); - const { content, action } = await itemsChange(Tasks, task, 'task', destinationStageId); + const { content, action } = await itemsChange(task, 'task', destinationStageId); await sendNotifications({ item: task, diff --git a/src/data/resolvers/mutations/tickets.ts b/src/data/resolvers/mutations/tickets.ts index 38cc10bd3..074e34e65 100644 --- a/src/data/resolvers/mutations/tickets.ts +++ b/src/data/resolvers/mutations/tickets.ts @@ -76,13 +76,15 @@ const ticketMutations = { { _id, destinationStageId }: { _id: string; destinationStageId: string }, { user }: IContext, ) { - const ticket = await Tickets.updateTicket(_id, { + const ticket = await Tickets.getTicket(_id); + + await Tickets.updateTicket(_id, { modifiedAt: new Date(), modifiedBy: user._id, stageId: destinationStageId, }); - const { content, action } = await itemsChange(Tickets, ticket, 'ticket', destinationStageId); + const { content, action } = await itemsChange(ticket, 'ticket', destinationStageId); await sendNotifications({ item: ticket, diff --git a/src/db/models/Boards.ts b/src/db/models/Boards.ts index 5ec63e92d..3e626c85f 100644 --- a/src/db/models/Boards.ts +++ b/src/db/models/Boards.ts @@ -91,6 +91,7 @@ const createOrUpdatePipelineStages = async (stages: IPipelineStage[], pipelineId }; export interface IBoardModel extends Model { + getBoard(_id: string): Promise; createBoard(doc: IBoard): Promise; updateBoard(_id: string, doc: IBoard): Promise; removeBoard(_id: string): void; @@ -98,6 +99,19 @@ export interface IBoardModel extends Model { export const loadBoardClass = () => { class Board { + /* + * Get a Board + */ + public static async getBoard(_id: string) { + const board = await Boards.findOne({ _id }); + + if (!board) { + throw new Error('Board not found'); + } + + return board; + } + /** * Create a board */