oneuptime/CommonServer/Services/StatusPagePrivateUserService.ts

90 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-11-22 20:18:12 +00:00
import PostgresDatabase from '../Infrastructure/PostgresDatabase';
import Model from 'Model/Models/StatusPagePrivateUser';
2022-12-19 08:53:45 +00:00
import DatabaseService, { OnCreate } from './DatabaseService';
import StatusPage from 'Model/Models/StatusPage';
import StatusPageService from './StatusPageService';
import ObjectID from 'Common/Types/ObjectID';
import BadDataException from 'Common/Types/Exception/BadDataException';
import OneUptimeDate from 'Common/Types/Date';
import MailService from './MailService';
import EmailTemplateType from 'Common/Types/Email/EmailTemplateType';
import URL from 'Common/Types/API/URL';
import logger from '../Utils/Logger';
import { Domain, FileRoute, HttpProtocol } from '../Config';
2022-11-22 20:18:12 +00:00
export class Service extends DatabaseService<Model> {
public constructor(postgresDatabase?: PostgresDatabase) {
super(Model, postgresDatabase);
}
2022-12-19 08:53:45 +00:00
protected override async onCreateSuccess(
_onCreate: OnCreate<Model>,
createdItem: Model
2022-12-19 09:39:55 +00:00
): Promise<Model> {
// send email to the user.
2022-12-19 14:36:50 +00:00
const token: string = ObjectID.generate().toString();
2022-12-19 14:09:21 +00:00
await this.updateOneById({
2022-12-19 14:36:50 +00:00
id: createdItem.id!,
2022-12-19 14:09:21 +00:00
data: {
2022-12-19 14:36:50 +00:00
resetPasswordToken: token,
resetPasswordExpires: OneUptimeDate.getOneDayAfter(),
2022-12-19 14:09:21 +00:00
},
props: {
2022-12-19 14:36:50 +00:00
isRoot: true,
ignoreHooks: true,
},
});
2022-12-19 08:53:45 +00:00
2022-12-19 09:39:55 +00:00
const statusPage: StatusPage | null =
await StatusPageService.findOneById({
id: createdItem.statusPageId!,
props: {
isRoot: true,
ignoreHooks: true,
},
select: {
_id: true,
name: true,
pageTitle: true,
logoFileId: true,
},
});
2022-12-19 08:53:45 +00:00
if (!statusPage) {
2022-12-19 09:39:55 +00:00
throw new BadDataException('Status Page not found');
2022-12-19 08:53:45 +00:00
}
2022-12-19 13:05:47 +00:00
const statusPageName: string | undefined =
statusPage.pageTitle || statusPage.name;
2022-12-19 08:53:45 +00:00
2022-12-19 09:39:55 +00:00
const statusPageURL: string = await StatusPageService.getStatusPageURL(
statusPage.id!
);
2022-12-19 08:53:45 +00:00
MailService.sendMail({
toEmail: createdItem.email!,
2022-12-19 09:39:55 +00:00
subject: 'You have been invited to ' + statusPageName,
2022-12-19 08:53:45 +00:00
templateType: EmailTemplateType.StatusPageWelcomeEmail,
vars: {
statusPageName: statusPageName!,
2022-12-19 14:09:21 +00:00
statusPageUrl: statusPageURL,
2022-12-19 08:53:45 +00:00
logoUrl: statusPage.logoFileId
2022-12-19 09:39:55 +00:00
? new URL(HttpProtocol, Domain)
2022-12-19 14:36:50 +00:00
.addRoute(FileRoute)
.addRoute('/image/' + statusPage.logoFileId)
.toString()
2022-12-19 09:39:55 +00:00
: '',
2022-12-19 08:53:45 +00:00
homeURL: statusPageURL,
2022-12-19 09:39:55 +00:00
tokenVerifyUrl: URL.fromString(statusPageURL)
2022-12-19 14:36:50 +00:00
.addRoute('/reset-password/' + token)
2022-12-19 09:39:55 +00:00
.toString(),
2022-12-19 08:53:45 +00:00
},
}).catch((err: Error) => {
logger.error(err);
});
return createdItem;
}
2022-11-22 20:18:12 +00:00
}
export default new Service();