oneuptime/CommonServer/Services/IncidentService.ts

110 lines
3.7 KiB
TypeScript
Raw Normal View History

2022-08-21 20:07:16 +00:00
import PostgresDatabase from '../Infrastructure/PostgresDatabase';
import Model from 'Model/Models/Incident';
2022-08-22 18:48:05 +00:00
import DatabaseService, { OnCreate } from './DatabaseService';
2022-08-22 18:59:57 +00:00
import ObjectID from 'Common/Types/ObjectID';
import Monitor from 'Model/Models/Monitor';
2022-08-24 17:17:58 +00:00
import MonitorService from './MonitorService';
import DatabaseCommonInteractionProps from 'Common/Types/Database/DatabaseCommonInteractionProps';
import IncidentStateTimeline from 'Model/Models/IncidentStateTimeline';
import IncidentStateTimelineService from './IncidentStateTimelineService';
2022-08-25 13:12:11 +00:00
import CreateBy from '../Types/Database/CreateBy';
import BadDataException from 'Common/Types/Exception/BadDataException';
import IncidentState from 'Model/Models/IncidentState';
import IncidentStateService from './IncidentStateService';
2022-08-21 20:07:16 +00:00
export class Service extends DatabaseService<Model> {
public constructor(postgresDatabase?: PostgresDatabase) {
super(Model, postgresDatabase);
}
2022-08-22 18:48:05 +00:00
2022-08-25 13:12:11 +00:00
protected override async onBeforeCreate(
createBy: CreateBy<Model>
): Promise<OnCreate<Model>> {
if (!createBy.props.tenantId) {
throw new BadDataException('ProjectId required to create monitor.');
}
const incidentState: IncidentState | null =
await IncidentStateService.findOneBy({
query: {
projectId: createBy.props.tenantId,
isCreatedState: true,
},
select: {
_id: true,
},
props: {
isRoot: true,
},
});
if (!incidentState || !incidentState.id) {
throw new BadDataException(
2022-10-18 20:51:38 +00:00
'Created incident state not found for this project. Please add created incident state from settings.'
2022-08-25 13:12:11 +00:00
);
}
createBy.data.currentIncidentStateId = incidentState.id;
return { createBy, carryForward: null };
}
2022-08-22 18:59:57 +00:00
protected override async onCreateSuccess(
onCreate: OnCreate<Model>,
createdItem: Model
): Promise<Model> {
2022-08-25 13:12:11 +00:00
if (!createdItem.projectId) {
2022-08-25 17:15:37 +00:00
throw new BadDataException('projectId is required');
2022-08-25 13:12:11 +00:00
}
if (!createdItem.id) {
2022-08-25 17:15:37 +00:00
throw new BadDataException('id is required');
2022-08-25 13:12:11 +00:00
}
if (!createdItem.currentIncidentStateId) {
2022-08-25 17:15:37 +00:00
throw new BadDataException('currentIncidentStateId is required');
2022-08-25 13:12:11 +00:00
}
2022-08-24 17:17:58 +00:00
if (createdItem.changeMonitorStatusToId && createdItem.projectId) {
2022-08-22 18:59:57 +00:00
// change status of all the monitors.
await MonitorService.changeMonitorStatus(
2022-08-24 17:17:58 +00:00
createdItem.projectId,
2022-08-22 18:59:57 +00:00
createdItem.monitors?.map((monitor: Monitor) => {
return new ObjectID(monitor._id || '');
}) || [],
createdItem.changeMonitorStatusToId,
onCreate.createBy.props
);
2022-08-22 18:48:05 +00:00
}
2022-08-25 17:15:37 +00:00
await this.changeIncidentState(
createdItem.projectId,
createdItem.id,
createdItem.currentIncidentStateId,
onCreate.createBy.props
);
2022-08-25 13:12:11 +00:00
2022-08-22 18:48:05 +00:00
return createdItem;
}
2022-08-24 17:17:58 +00:00
public async changeIncidentState(
projectId: ObjectID,
incidentId: ObjectID,
incidentStateId: ObjectID,
props: DatabaseCommonInteractionProps
): Promise<void> {
2022-08-25 17:15:37 +00:00
const statusTimeline: IncidentStateTimeline =
new IncidentStateTimeline();
2022-08-24 17:17:58 +00:00
statusTimeline.incidentId = incidentId;
statusTimeline.incidentStateId = incidentStateId;
statusTimeline.projectId = projectId;
await IncidentStateTimelineService.create({
data: statusTimeline,
2022-08-25 17:15:37 +00:00
props: props,
});
2022-08-24 17:17:58 +00:00
}
2022-08-21 20:07:16 +00:00
}
export default new Service();