Skip to content

Commit

Permalink
refactor: clean up whitespace and improve formatting in workspace uti…
Browse files Browse the repository at this point in the history
…lity classes
  • Loading branch information
simlarsen committed Feb 14, 2025
1 parent c20fbcc commit cd97a72
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 123 deletions.
30 changes: 13 additions & 17 deletions Common/Server/Services/IncidentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,8 @@ import {
WorkspaceMessageBlock,
WorkspacePayloadMarkdown,
} from "../../Types/Workspace/WorkspaceMessagePayload";
import WorkspaceProjectAuthToken from "../../Models/DatabaseModels/WorkspaceProjectAuthToken";
import WorkspaceProjectAuthTokenService from "./WorkspaceProjectAuthTokenService";
import WorkspaceNotificationRuleService from "./WorkspaceNotificationRuleService";
import NotificationRuleEventType from "../../Types/Workspace/NotificationRules/EventType";
import WorkspaceNotificationRule from "../../Models/DatabaseModels/WorkspaceNotificationRule";
import CreateChannelNotificationRule from "../../Types/Workspace/NotificationRules/CreateChannelNotificationRule";

export class Service extends DatabaseService<Model> {
public constructor() {
Expand Down Expand Up @@ -1330,20 +1326,20 @@ ${incidentSeverity.name}
incidentId: ObjectID;
incidentNumber: number;
}): Promise<void> {

// we will notify the workspace about the incident creation with the bot tokken which is in WorkspaceProjectAuth Table.
await WorkspaceNotificationRuleService.createInviteAndPostToChannelsBasedOnRules({
projectId: data.projectId,
notificationFor: {
incidentId: data.incidentId
},
notificationRuleEventType: NotificationRuleEventType.Incident,
channelNameSiffix: data.incidentNumber.toString(),
messageBlocks: await this.getWorkspaceMessageBlocksForIncidentCreate({
incidentId: data.incidentId
})
});

await WorkspaceNotificationRuleService.createInviteAndPostToChannelsBasedOnRules(
{
projectId: data.projectId,
notificationFor: {
incidentId: data.incidentId,
},
notificationRuleEventType: NotificationRuleEventType.Incident,
channelNameSiffix: data.incidentNumber.toString(),
messageBlocks: await this.getWorkspaceMessageBlocksForIncidentCreate({
incidentId: data.incidentId,
}),
},
);
}

public async getWorkspaceMessageBlocksForIncidentCreate(data: {
Expand Down
120 changes: 53 additions & 67 deletions Common/Server/Services/WorkspaceNotificationRuleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import { WorkspaceChannel } from "../Utils/Workspace/WorkspaceBase";
import WorkspaceUtil from "../Utils/Workspace/Workspace";
import WorkspaceUserAuthToken from "../../Models/DatabaseModels/WorkspaceUserAuthToken";
import WorkspaceUserAuthTokenService from "./WorkspaceUserAuthTokenService";
import WorkspaceMessagePayload, { WorkspaceMessageBlock } from "../../Types/Workspace/WorkspaceMessagePayload";
import WorkspaceMessagePayload, {
WorkspaceMessageBlock,
} from "../../Types/Workspace/WorkspaceMessagePayload";
import WorkspaceProjectAuthToken from "../../Models/DatabaseModels/WorkspaceProjectAuthToken";
import WorkspaceProjectAuthTokenService from "./WorkspaceProjectAuthTokenService";

Expand All @@ -42,16 +44,13 @@ export class Service extends DatabaseService<Model> {
super(Model);
}


public async createInviteAndPostToChannelsBasedOnRules(data: {
projectId: ObjectID;
notificationRuleEventType: NotificationRuleEventType;
notificationFor: NotificationFor;
channelNameSiffix: string;
messageBlocks: Array<WorkspaceMessageBlock>
messageBlocks: Array<WorkspaceMessageBlock>;
}): Promise<void> {


const projectAuths: Array<WorkspaceProjectAuthToken> =
await WorkspaceProjectAuthTokenService.getProjectAuths({
projectId: data.projectId,
Expand All @@ -63,12 +62,11 @@ export class Service extends DatabaseService<Model> {
}

for (const projectAuth of projectAuths) {

if(!projectAuth.authToken){
if (!projectAuth.authToken) {
continue;
}

if(!projectAuth.workspaceType){
if (!projectAuth.workspaceType) {
continue;
}

Expand All @@ -87,7 +85,6 @@ export class Service extends DatabaseService<Model> {
return;
}


const createdWorkspaceChannels: Array<WorkspaceChannel> =
await this.createChannelsBasedOnRules({
projectOrUserAuthTokenForWorkspasce: authToken,
Expand All @@ -105,12 +102,13 @@ export class Service extends DatabaseService<Model> {
notificationRules: notificationRules.map((rule: Model) => {
return rule.notificationRule as CreateChannelNotificationRule;
}),
channelNames: createdWorkspaceChannels.map((channel: WorkspaceChannel) => {
return channel.name;
}),
channelNames: createdWorkspaceChannels.map(
(channel: WorkspaceChannel) => {
return channel.name;
},
),
});


const existingChannelNames: Array<string> =
this.getExistingChannelNamesFromNotificationRules({
notificationRules: notificationRules.map((rule: Model) => {
Expand All @@ -131,35 +129,30 @@ export class Service extends DatabaseService<Model> {
workspaceMessagePayload: {
_type: "WorkspaceMessagePayload",
channelNames: existingChannelNames,
messageBlocks: data.messageBlocks
}
messageBlocks: data.messageBlocks,
},
});
}
}


public async postToWorkspaceChannels(data: {
projectOrUserAuthTokenForWorkspasce: string;
workspaceType: WorkspaceType;
workspaceMessagePayload: WorkspaceMessagePayload
workspaceMessagePayload: WorkspaceMessagePayload;
}): Promise<void> {

await WorkspaceUtil.getWorkspaceTypeUtil(data.workspaceType).sendMessage({
workspaceMessagePayload: data.workspaceMessagePayload,
authToken: data.projectOrUserAuthTokenForWorkspasce,
});

}


public async inviteUsersAndTeamsToChannelsBasedOnRules(data: {
projectId: ObjectID;
projectOrUserAuthTokenForWorkspasce: string;
workspaceType: WorkspaceType;
notificationRules: Array<CreateChannelNotificationRule>;
channelNames: Array<string>;
}): Promise<void> {

const inviteUserIds: Array<ObjectID> =
await this.getUsersIdsToInviteToChannel({
notificationRules: data.notificationRules,
Expand All @@ -168,47 +161,48 @@ export class Service extends DatabaseService<Model> {
const workspaceUserIds: Array<string> = [];

for (const userId of inviteUserIds) {
const workspaceUserId: string | null = await this.getWorkspaceUserIdFromOneUptimeUserId({
projectId: data.projectId,
workspaceType: data.workspaceType,
oneupitmeUserId: userId,
});
const workspaceUserId: string | null =
await this.getWorkspaceUserIdFromOneUptimeUserId({
projectId: data.projectId,
workspaceType: data.workspaceType,
oneupitmeUserId: userId,
});

if (workspaceUserId) {
workspaceUserIds.push(workspaceUserId);
}
}


await WorkspaceUtil.getWorkspaceTypeUtil(data.workspaceType).inviteUsersToChannels({
await WorkspaceUtil.getWorkspaceTypeUtil(
data.workspaceType,
).inviteUsersToChannels({
authToken: data.projectOrUserAuthTokenForWorkspasce,
workspaceChannelInvitationPayload: {
channelNames: data.channelNames,
workspaceUserIds: workspaceUserIds,
}
},
});

}


public async getWorkspaceUserIdFromOneUptimeUserId(data: {
projectId: ObjectID;
workspaceType: WorkspaceType;
oneupitmeUserId: ObjectID;
}): Promise<string | null> {
const userAuth: WorkspaceUserAuthToken | null = await WorkspaceUserAuthTokenService.findOneBy({
query: {
projectId: data.projectId,
workspaceType: data.workspaceType,
userId: data.oneupitmeUserId,
},
select: {
workspaceUserId: true,
},
props: {
isRoot: true,
},
});
const userAuth: WorkspaceUserAuthToken | null =
await WorkspaceUserAuthTokenService.findOneBy({
query: {
projectId: data.projectId,
workspaceType: data.workspaceType,
userId: data.oneupitmeUserId,
},
select: {
workspaceUserId: true,
},
props: {
isRoot: true,
},
});

if (!userAuth) {
return null;
Expand All @@ -217,40 +211,36 @@ export class Service extends DatabaseService<Model> {
return userAuth.workspaceUserId?.toString() || null;
}


public async createChannelsBasedOnRules(data: {
projectOrUserAuthTokenForWorkspasce: string;
workspaceType: WorkspaceType;
notificationRules: Array<CreateChannelNotificationRule>;
channelNameSiffix: string;
}): Promise<Array<WorkspaceChannel>> {


const createdWorkspaceChannels: Array<WorkspaceChannel> = [];
const createdChannelNames: Array<string> = [];

const newChannelNames: Array<string> =
this.getNewChannelNamesFromNotificationRules(
{
notificationRules: data.notificationRules,
channelNameSiffix: data.channelNameSiffix
},
);
this.getNewChannelNamesFromNotificationRules({
notificationRules: data.notificationRules,
channelNameSiffix: data.channelNameSiffix,
});

if (!newChannelNames || newChannelNames.length === 0) {
return [];
}

for (const newChannelName of newChannelNames) {

// if already created then skip it.
if (createdChannelNames.includes(newChannelName)) {
continue;
}

// create channel.
const channel: WorkspaceChannel =
await WorkspaceUtil.getWorkspaceTypeUtil(data.workspaceType).createChannel({
await WorkspaceUtil.getWorkspaceTypeUtil(
data.workspaceType,
).createChannel({
authToken: data.projectOrUserAuthTokenForWorkspasce,
channelName: newChannelName,
});
Expand All @@ -263,17 +253,13 @@ export class Service extends DatabaseService<Model> {
return createdWorkspaceChannels;
}


public async getUsersIdsToInviteToChannel(data: {
notificationRules: Array<
CreateChannelNotificationRule
>;
notificationRules: Array<CreateChannelNotificationRule>;
}): Promise<Array<ObjectID>> {
const inviteUserIds: Array<ObjectID> = [];

for (const notificationRule of data.notificationRules) {
const workspaceRules:
CreateChannelNotificationRule = notificationRule;
const workspaceRules: CreateChannelNotificationRule = notificationRule;

if (workspaceRules.shouldCreateNewChannel) {
if (
Expand Down Expand Up @@ -410,9 +396,9 @@ export class Service extends DatabaseService<Model> {
notificationFor: NotificationFor;
}): Promise<{
[key in NotificationRuleConditionCheckOn]:
| string
| Array<string>
| undefined;
| string
| Array<string>
| undefined;
}> {
if (data.notificationFor.incidentId) {
const incident: Incident | null = await IncidentService.findOneById({
Expand Down Expand Up @@ -682,9 +668,9 @@ export class Service extends DatabaseService<Model> {

const values: {
[key in NotificationRuleConditionCheckOn]:
| string
| Array<string>
| undefined;
| string
| Array<string>
| undefined;
} = await this.getValuesBasedOnNotificationFor({
notificationFor: data.notificationFor,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import WorkspaceBase from "../WorkspaceBase";

export default class MicrosoftTeams extends WorkspaceBase {
}
export default class MicrosoftTeams extends WorkspaceBase {}
1 change: 0 additions & 1 deletion Common/Server/Utils/Workspace/Slack/Slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import BadRequestException from "../../../../Types/Exception/BadRequestException
import WorkspaceBase, { WorkspaceChannel } from "../WorkspaceBase";

export default class SlackUtil extends WorkspaceBase {

public static override async inviteUserToChannel(data: {
authToken: string;
channelName: string;
Expand Down
26 changes: 15 additions & 11 deletions Common/Server/Utils/Workspace/Workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import SlackWorkspace from "./Slack/Slack";
import MicrosoftTeamsWorkspace from "./MicrosoftTeams/MicrosoftTeams";
import BadDataException from "../../../Types/Exception/BadDataException";

export default class WorkspaceUtil {
public static getWorkspaceTypeUtil(workspaceType: WorkspaceType): typeof WorkspaceBase {
if(workspaceType === WorkspaceType.Slack){
return SlackWorkspace;
}
export default class WorkspaceUtil {
public static getWorkspaceTypeUtil(
workspaceType: WorkspaceType,
): typeof WorkspaceBase {
if (workspaceType === WorkspaceType.Slack) {
return SlackWorkspace;
}

if(workspaceType === WorkspaceType.MicrosoftTeams){
return MicrosoftTeamsWorkspace;
}
if (workspaceType === WorkspaceType.MicrosoftTeams) {
return MicrosoftTeamsWorkspace;
}

throw new BadDataException(`Workspace type ${workspaceType} is not supported`);
}
}
throw new BadDataException(
`Workspace type ${workspaceType} is not supported`,
);
}
}
Loading

0 comments on commit cd97a72

Please sign in to comment.