Files
oneuptime/Common/Server/Services/IncidentEpisodePublicNoteService.ts

266 lines
8.2 KiB
TypeScript

import CreateBy from "../Types/Database/CreateBy";
import { OnCreate, OnUpdate } from "../Types/Database/Hooks";
import DatabaseService from "./DatabaseService";
import OneUptimeDate from "../../Types/Date";
import Model from "../../Models/DatabaseModels/IncidentEpisodePublicNote";
import IncidentEpisodeFeedService from "./IncidentEpisodeFeedService";
import { IncidentEpisodeFeedEventType } from "../../Models/DatabaseModels/IncidentEpisodeFeed";
import { Blue500, Indigo500 } from "../../Types/BrandColors";
import ObjectID from "../../Types/ObjectID";
import { LIMIT_PER_PROJECT } from "../../Types/Database/LimitMax";
import IncidentEpisodeService from "./IncidentEpisodeService";
import IncidentEpisode from "../../Models/DatabaseModels/IncidentEpisode";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
import StatusPageSubscriberNotificationStatus from "../../Types/StatusPage/StatusPageSubscriberNotificationStatus";
import File from "../../Models/DatabaseModels/File";
import FileAttachmentMarkdownUtil from "../Utils/FileAttachmentMarkdownUtil";
export class Service extends DatabaseService<Model> {
public constructor() {
super(Model);
}
@CaptureSpan()
public async addNote(data: {
userId: ObjectID;
incidentEpisodeId: ObjectID;
projectId: ObjectID;
note: string;
attachmentFileIds?: Array<ObjectID>;
postedFromSlackMessageId?: string;
}): Promise<Model> {
const publicNote: Model = new Model();
publicNote.createdByUserId = data.userId;
publicNote.incidentEpisodeId = data.incidentEpisodeId;
publicNote.projectId = data.projectId;
publicNote.note = data.note;
publicNote.postedAt = OneUptimeDate.getCurrentDate();
if (data.postedFromSlackMessageId) {
publicNote.postedFromSlackMessageId = data.postedFromSlackMessageId;
}
if (data.attachmentFileIds && data.attachmentFileIds.length > 0) {
publicNote.attachments = data.attachmentFileIds.map(
(fileId: ObjectID) => {
const file: File = new File();
file.id = fileId;
return file;
},
);
}
return this.create({
data: publicNote,
props: {
isRoot: true,
},
});
}
@CaptureSpan()
public async hasNoteFromSlackMessage(data: {
incidentEpisodeId: ObjectID;
postedFromSlackMessageId: string;
}): Promise<boolean> {
const existingNote: Model | null = await this.findOneBy({
query: {
incidentEpisodeId: data.incidentEpisodeId,
postedFromSlackMessageId: data.postedFromSlackMessageId,
},
select: {
_id: true,
},
props: {
isRoot: true,
},
});
return existingNote !== null;
}
@CaptureSpan()
protected override async onBeforeCreate(
createBy: CreateBy<Model>,
): Promise<OnCreate<Model>> {
if (!createBy.data.postedAt) {
createBy.data.postedAt = OneUptimeDate.getCurrentDate();
}
// Set notification status based on shouldStatusPageSubscribersBeNotifiedOnNoteCreated
if (
createBy.data.shouldStatusPageSubscribersBeNotifiedOnNoteCreated === false
) {
createBy.data.subscriberNotificationStatusOnNoteCreated =
StatusPageSubscriberNotificationStatus.Skipped;
createBy.data.subscriberNotificationStatusMessage =
"Notifications skipped as subscribers are not to be notified for this episode note.";
} else if (
createBy.data.shouldStatusPageSubscribersBeNotifiedOnNoteCreated === true
) {
createBy.data.subscriberNotificationStatusOnNoteCreated =
StatusPageSubscriberNotificationStatus.Pending;
}
return {
createBy: createBy,
carryForward: null,
};
}
@CaptureSpan()
public override async onCreateSuccess(
_onCreate: OnCreate<Model>,
createdItem: Model,
): Promise<Model> {
const userId: ObjectID | null | undefined =
createdItem.createdByUserId || createdItem.createdByUser?.id;
const incidentEpisodeId: ObjectID = createdItem.incidentEpisodeId!;
const projectId: ObjectID = createdItem.projectId!;
const episodeNumberResult: {
number: number | null;
numberWithPrefix: string | null;
} = await IncidentEpisodeService.getEpisodeNumber({
episodeId: incidentEpisodeId,
});
const attachmentsMarkdown: string = await this.getAttachmentsMarkdown(
createdItem.id!,
"/incident-episode-public-note/attachment",
);
await IncidentEpisodeFeedService.createIncidentEpisodeFeedItem({
incidentEpisodeId: createdItem.incidentEpisodeId!,
projectId: createdItem.projectId!,
incidentEpisodeFeedEventType: IncidentEpisodeFeedEventType.PublicNote,
displayColor: Indigo500,
userId: userId || undefined,
feedInfoInMarkdown: `📄 posted **public note** for this [Episode ${episodeNumberResult.numberWithPrefix || "#" + episodeNumberResult.number}](${(await IncidentEpisodeService.getEpisodeLinkInDashboard(projectId!, incidentEpisodeId!)).toString()}) on status page:
${(createdItem.note || "") + attachmentsMarkdown}
`,
workspaceNotification: {
sendWorkspaceNotification: true,
notifyUserId: userId || undefined,
},
});
return createdItem;
}
@CaptureSpan()
public override async onUpdateSuccess(
onUpdate: OnUpdate<Model>,
_updatedItemIds: Array<ObjectID>,
): Promise<OnUpdate<Model>> {
if (onUpdate.updateBy.data.note) {
const updatedItems: Array<Model> = await this.findBy({
query: onUpdate.updateBy.query,
limit: LIMIT_PER_PROJECT,
skip: 0,
props: {
isRoot: true,
},
select: {
incidentEpisodeId: true,
projectId: true,
incidentEpisode: {
_id: true,
episodeNumber: true,
episodeNumberWithPrefix: true,
projectId: true,
},
note: true,
createdByUserId: true,
createdByUser: {
_id: true,
},
},
});
const userId: ObjectID | null | undefined =
onUpdate.updateBy.props.userId;
for (const updatedItem of updatedItems) {
const episode: IncidentEpisode = updatedItem.incidentEpisode!;
const attachmentsMarkdown: string = await this.getAttachmentsMarkdown(
updatedItem.id!,
"/incident-episode-public-note/attachment",
);
await IncidentEpisodeFeedService.createIncidentEpisodeFeedItem({
incidentEpisodeId: updatedItem.incidentEpisodeId!,
projectId: updatedItem.projectId!,
incidentEpisodeFeedEventType: IncidentEpisodeFeedEventType.PublicNote,
displayColor: Blue500,
userId: userId || undefined,
feedInfoInMarkdown: `📄 updated **Public Note** for this [Episode ${episode.episodeNumberWithPrefix || "#" + episode.episodeNumber}](${(await IncidentEpisodeService.getEpisodeLinkInDashboard(episode.projectId!, episode.id!)).toString()})
${(updatedItem.note || "") + attachmentsMarkdown}
`,
workspaceNotification: {
sendWorkspaceNotification: true,
notifyUserId: userId || undefined,
},
});
}
}
return onUpdate;
}
private async getAttachmentsMarkdown(
modelId: ObjectID,
attachmentApiPath: string,
): Promise<string> {
if (!modelId) {
return "";
}
const noteWithAttachments: Model | null = await this.findOneById({
id: modelId,
select: {
attachments: {
_id: true,
},
},
props: {
isRoot: true,
},
});
if (!noteWithAttachments || !noteWithAttachments.attachments) {
return "";
}
const attachmentIds: Array<ObjectID> = noteWithAttachments.attachments
.map((file: File) => {
if (file.id) {
return file.id;
}
if (file._id) {
return new ObjectID(file._id);
}
return null;
})
.filter((id: ObjectID | null): id is ObjectID => {
return Boolean(id);
});
if (!attachmentIds.length) {
return "";
}
return await FileAttachmentMarkdownUtil.buildAttachmentMarkdown({
modelId,
attachmentIds,
attachmentApiPath,
});
}
}
export default new Service();