add server to monorepo.

This commit is contained in:
a.bouhuolia
2023-02-03 11:57:50 +02:00
parent 28e309981b
commit 80b97b5fdc
1303 changed files with 137049 additions and 0 deletions

View File

@@ -0,0 +1,110 @@
import { Inject, Service } from 'typedi';
import { Router, NextFunction, Response } from 'express';
import { check } from 'express-validator';
import { Request } from 'express-validator/src/base';
import EasySmsIntegration from '@/services/SmsIntegration/EasySmsIntegration';
import BaseController from '../BaseController';
@Service()
export default class EasySmsIntegrationController extends BaseController {
@Inject()
easySmsIntegrationService: EasySmsIntegration;
/**
* Controller router.
*/
public router = () => {
const router = Router();
router.post(
'/easysms/integrate',
[check('token').exists()],
this.integrationEasySms
);
router.post(
'/easysms/disconnect',
this.disconnectEasysms
)
router.get('/easysms', this.getIntegrationMeta);
return router;
};
/**
* Easysms integration API.
* @param {Request} req - Request object.
* @param {Response} res - Response object.
* @param {NextFunction} next - Next function.
*/
private integrationEasySms = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { tenantId } = req;
const easysmsIntegrateDTO = this.matchedBodyData(req);
try {
await this.easySmsIntegrationService.integrate(
tenantId,
easysmsIntegrateDTO
);
return res.status(200).send({
message:
'The system has been integrated with Easysms sms gateway successfully.',
});
} catch (error) {
next(error);
}
};
/**
* Retrieve the Easysms integration meta.
* @param req
* @param res
* @param next
* @returns
*/
private getIntegrationMeta = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { tenantId } = req;
try {
const data = await this.easySmsIntegrationService.getIntegrationMeta(
tenantId
);
return res.status(200).send({ data });
} catch (error) {
next(error);
}
};
/**
*
* @param req
* @param res
* @param next
* @returns
*/
private disconnectEasysms = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { tenantId } = req;
try {
await this.easySmsIntegrationService.disconnect(
tenantId,
);
return res.status(200).send({
message: 'The sms gateway integration has been disconnected successfully.',
});
} catch (error) {
next(error);
}
}
}

View File

@@ -0,0 +1,114 @@
import { Inject, Service } from 'typedi';
import { Router, Request, Response } from 'express';
import { body, query } from 'express-validator';
import { pick } from 'lodash';
import { IOptionDTO, IOptionsDTO } from '@/interfaces';
import BaseController from '@/api/controllers/BaseController';
import asyncMiddleware from '@/api/middleware/asyncMiddleware';
import { AbilitySubject, PreferencesAction } from '@/interfaces';
import SettingsService from '@/services/Settings/SettingsService';
import CheckPolicies from '@/api/middleware/CheckPolicies';
@Service()
export default class SettingsController extends BaseController {
@Inject()
settingsService: SettingsService;
/**
* Router constructor.
*/
router() {
const router = Router();
router.post(
'/',
CheckPolicies(PreferencesAction.Mutate, AbilitySubject.Preferences),
this.saveSettingsValidationSchema,
this.validationResult,
asyncMiddleware(this.saveSettings.bind(this))
);
router.get(
'/',
this.getSettingsSchema,
this.validationResult,
asyncMiddleware(this.getSettings.bind(this))
);
return router;
}
/**
* Save settings validation schema.
*/
private get saveSettingsValidationSchema() {
return [
body('options').isArray({ min: 1 }),
body('options.*.key').exists().trim().isLength({ min: 1 }),
body('options.*.value').exists().trim(),
body('options.*.group').exists().trim().isLength({ min: 1 }),
];
}
/**
* Retrieve the application options from the storage.
*/
private get getSettingsSchema() {
return [
query('key').optional().trim().escape(),
query('group').optional().trim().escape(),
];
}
/**
* Saves the given options to the storage.
* @param {Request} req -
* @param {Response} res -
*/
public async saveSettings(req: Request, res: Response, next) {
const { tenantId } = req;
const optionsDTO: IOptionsDTO = this.matchedBodyData(req);
const { settings } = req;
const errorReasons: { type: string; code: number; keys: [] }[] = [];
const notDefinedOptions = this.settingsService.validateNotDefinedSettings(
tenantId,
optionsDTO.options
);
if (notDefinedOptions.length) {
errorReasons.push({
type: 'OPTIONS.KEY.NOT.DEFINED',
code: 200,
keys: notDefinedOptions.map((o) => ({ ...pick(o, ['key', 'group']) })),
});
}
if (errorReasons.length) {
return res.status(400).send({ errors: errorReasons });
}
optionsDTO.options.forEach((option: IOptionDTO) => {
settings.set({ ...option });
});
try {
await settings.save();
return res.status(200).send({
type: 'success',
code: 'OPTIONS.SAVED.SUCCESSFULLY',
message: 'Options have been saved successfully.',
});
} catch (error) {
next(error);
}
}
/**
* Retrieve settings.
* @param {Request} req
* @param {Response} res
*/
public getSettings(req: Request, res: Response) {
const { settings } = req;
const allSettings = settings.all();
return res.status(200).send({ settings: allSettings });
}
}

View File

@@ -0,0 +1,168 @@
import { Inject, Service } from 'typedi';
import { check, oneOf, param } from 'express-validator';
import { Router, Response, Request, NextFunction } from 'express';
import SmsNotificationsSettingsService from '@/services/Settings/SmsNotificationsSettings';
import BaseController from '../BaseController';
import { ServiceError } from '@/exceptions';
import {
AbilitySubject,
PreferencesAction,
IEditSmsNotificationDTO,
} from '@/interfaces';
import CheckPolicies from '@/api/middleware/CheckPolicies';
@Service()
export default class SettingsController extends BaseController {
@Inject()
smsNotificationsSettings: SmsNotificationsSettingsService;
/**
* Router constructor.
*/
router() {
const router = Router();
router.get(
'/sms-notifications',
[],
this.validationResult,
this.asyncMiddleware(this.smsNotifications),
this.handleServiceErrors
);
router.get(
'/sms-notification/:notification_key',
[param('notification_key').exists().isString()],
this.validationResult,
this.asyncMiddleware(this.smsNotification),
this.handleServiceErrors
);
router.post(
'/sms-notification',
CheckPolicies(PreferencesAction.Mutate, AbilitySubject.Preferences),
[
check('notification_key').exists(),
oneOf([
check('message_text').exists(),
check('is_notification_enabled').exists().isBoolean().toBoolean(),
]),
],
this.validationResult,
this.asyncMiddleware(this.updateSmsNotification),
this.handleServiceErrors
);
return router;
}
/**
* Retrieve the sms notifications.
* @param {Request} req -
* @param {Response} res -
* @param {NextFunction} next -
*/
private smsNotifications = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { tenantId } = req;
try {
const notifications =
await this.smsNotificationsSettings.smsNotificationsList(tenantId);
return res.status(200).send({ notifications });
} catch (error) {
next(error);
}
};
/**
* Retrieve the sms notification details from the given notification key.
* @param {Request} req -
* @param {Response} res -
* @param {NextFunction} next -
*/
private smsNotification = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { tenantId } = req;
const { notification_key: notificationKey } = req.params;
try {
const notification =
await this.smsNotificationsSettings.getSmsNotificationMeta(
tenantId,
notificationKey
);
return res.status(200).send({ notification });
} catch (error) {
next(error);
}
};
/**
* Update the given sms notification key.
* @param {Request} req -
* @param {Response} res -
* @param {NextFunction} next -
*/
private updateSmsNotification = async (
req: Request,
res: Response,
next: NextFunction
) => {
const { tenantId } = req;
const editDTO: IEditSmsNotificationDTO = this.matchedBodyData(req);
try {
await this.smsNotificationsSettings.editSmsNotificationMessage(
tenantId,
editDTO
);
return res.status(200).send({
message: 'Sms notification settings has been updated successfully.',
});
} catch (error) {
next(error);
}
};
/**
* Handles service errors.
* @param {Error} error
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
private handleServiceErrors = (
error: Error,
req: Request,
res: Response,
next: NextFunction
) => {
if (error instanceof ServiceError) {
if (error.errorType === 'SMS_NOTIFICATION_KEY_NOT_FOUND') {
return res.boom.badRequest(null, {
errors: [{ type: 'SMS_NOTIFICATION_KEY_NOT_FOUND', code: 1000 }],
});
}
if (error.errorType === 'UNSUPPORTED_SMS_MESSAGE_VARIABLES') {
return res.boom.badRequest(null, {
errors: [
{
type: 'UNSUPPORTED_SMS_MESSAGE_VARIABLES',
code: 1100,
data: { ...error.payload },
},
],
});
}
}
next(error);
};
}

View File

@@ -0,0 +1,23 @@
import { Router } from 'express';
import { Container, Service } from 'typedi';
import SmsNotificationSettings from './SmsNotificationsSettings';
import Settings from './Settings';
import EasySmsIntegrationController from './EasySmsIntegration';
import { AbilitySubject, PreferencesAction } from '@/interfaces';
import CheckPolicies from '@/api/middleware/CheckPolicies';
@Service()
export default class SettingsController {
/**
* Router constructor.
*/
router() {
const router = Router();
router.use('/', Container.get(EasySmsIntegrationController).router());
router.use('/', Container.get(SmsNotificationSettings).router());
router.use('/', Container.get(Settings).router());
return router;
}
}