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

Commit

Permalink
little refactor for growth hack mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
munkhjin0223 committed Sep 16, 2019
1 parent ebafe1f commit 79713f0
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/data/resolvers/mutations/growthHacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { IGrowthHack } from '../../../db/models/definitions/growthHacks';
import { IUserDocument } from '../../../db/models/definitions/users';
import { checkPermission } from '../../permissions/wrappers';
import { putCreateLog, putDeleteLog, putUpdateLog } from '../../utils';
import { itemsChange, sendNotifications } from '../boardUtils';
import { IBoardNotificationParams, itemsChange, sendNotifications } from '../boardUtils';
import { checkUserIds } from './notifications';

interface IGrowthHacksEdit extends IGrowthHack {
Expand All @@ -14,7 +14,7 @@ interface IGrowthHacksEdit extends IGrowthHack {

const growthHackMutations = {
/**
* Create new growthHack
* Create new growth hack
*/
async growthHacksAdd(_root, doc: IGrowthHack, { user }: { user: IUserDocument }) {
doc.initialStageId = doc.stageId;
Expand Down Expand Up @@ -46,7 +46,7 @@ const growthHackMutations = {
},

/**
* Edit growthHack
* Edit a growth hack
*/
async growthHacksEdit(_root, { _id, ...doc }: IGrowthHacksEdit, { user }) {
const oldGrowthHack = await GrowthHacks.getGrowthHack(_id);
Expand All @@ -57,21 +57,26 @@ const growthHackMutations = {
modifiedBy: user._id,
});

const { addedUserIds, removedUserIds } = checkUserIds(
oldGrowthHack.assignedUserIds || [],
doc.assignedUserIds || [],
);

await sendNotifications({
const notificationDoc: IBoardNotificationParams = {
item: updatedGrowthHack,
user,
type: NOTIFICATION_TYPES.GROWTH_HACK_EDIT,
invitedUsers: addedUserIds,
removedUsers: removedUserIds,
action: `has updated growthHack`,
action: `has updated a growth hack`,
content: `${updatedGrowthHack.name}`,
contentType: 'growthHack',
});
};

if (doc.assignedUserIds) {
const { addedUserIds, removedUserIds } = checkUserIds(
oldGrowthHack.assignedUserIds || [],
doc.assignedUserIds || [],
);

notificationDoc.invitedUsers = addedUserIds;
notificationDoc.removedUsers = removedUserIds;
}

await sendNotifications(notificationDoc);

if (updatedGrowthHack) {
await putUpdateLog(
Expand All @@ -88,7 +93,7 @@ const growthHackMutations = {
},

/**
* Change growthHack
* Change a growth hack
*/
async growthHacksChange(
_root,
Expand Down Expand Up @@ -122,14 +127,14 @@ const growthHackMutations = {
},

/**
* Update growthHack orders (not sendNotifaction, ordered card to change)
* Update growth hack orders (not sendNotifaction, ordered card to change)
*/
growthHacksUpdateOrder(_root, { stageId, orders }: { stageId: string; orders: IOrderInput[] }) {
return GrowthHacks.updateOrder(stageId, orders);
},

/**
* Remove growthHack
* Remove a growth hack
*/
async growthHacksRemove(_root, { _id }: { _id: string }, { user }: { user: IUserDocument }) {
const growthHack = await GrowthHacks.findOne({ _id });
Expand Down Expand Up @@ -162,7 +167,7 @@ const growthHackMutations = {
},

/**
* Watch growthHack
* Watch a growth hack
*/
async growthHacksWatch(_root, { _id, isAdd }: { _id: string; isAdd: boolean }, { user }: { user: IUserDocument }) {
const growthHack = await GrowthHacks.findOne({ _id });
Expand Down

0 comments on commit 79713f0

Please sign in to comment.