first commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import {
|
||||
FindSchedulesEnabledParams,
|
||||
ISchedulesEnabledAdapter,
|
||||
ISchedulesEnabled,
|
||||
CreateSchedulesEnabledParams,
|
||||
} from "./SchedulesEnabled.Interface";
|
||||
import { Document, Model, Schema, model } from "mongoose";
|
||||
|
||||
export interface ISchedulesEnabledDocument extends Omit<ISchedulesEnabled, "id">, Document {}
|
||||
|
||||
export class SchedulesEnabledAdapterMongoose implements ISchedulesEnabledAdapter {
|
||||
schema: Schema;
|
||||
schedulesEnabledList: Model<ISchedulesEnabledDocument>;
|
||||
|
||||
constructor() {
|
||||
this.schema = new Schema({
|
||||
employeeId: { type: Schema.Types.ObjectId, required: true, ref: "Employee" },
|
||||
companyId: { type: Schema.Types.ObjectId, required: true, ref: "Companie" },
|
||||
startDate: { type: Date, required: true },
|
||||
endDate: { type: Date, required: true },
|
||||
creationDate: { type: Date, required: true, default: Date.now },
|
||||
});
|
||||
|
||||
this.schedulesEnabledList = model<ISchedulesEnabledDocument>(
|
||||
"SchedulesEnabled",
|
||||
this.schema
|
||||
);
|
||||
}
|
||||
|
||||
public async create(data: CreateSchedulesEnabledParams): Promise<ISchedulesEnabled> {
|
||||
return await this.schedulesEnabledList.create(data);
|
||||
}
|
||||
|
||||
public async delete(id: string): Promise<void> {
|
||||
await this.schedulesEnabledList.deleteOne({ _id: id }).exec();
|
||||
}
|
||||
|
||||
public async findOne(
|
||||
filters: FindSchedulesEnabledParams
|
||||
): Promise<ISchedulesEnabledDocument | null> {
|
||||
return this.schedulesEnabledList.findOne(filters).exec();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
export type FindSchedulesEnabledParams = {
|
||||
_id?: string;
|
||||
companyId: string;
|
||||
employeeId?: string;
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
};
|
||||
|
||||
export type CheckSchedulesEnabledParams = {
|
||||
companyId: string;
|
||||
employeeId?: string;
|
||||
dateDay: Date;
|
||||
};
|
||||
|
||||
export type PaginateSchedulesEnabledParams = FindSchedulesEnabledParams & {
|
||||
page: number;
|
||||
limit: number;
|
||||
};
|
||||
|
||||
export type PaginateSchedulesEnabledResults = {
|
||||
data: ISchedulesEnabled[];
|
||||
page: number;
|
||||
pages: number;
|
||||
};
|
||||
|
||||
export type CreateSchedulesEnabledParams = {
|
||||
companyId: string;
|
||||
employeeId: string;
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
sessionUser: string;
|
||||
};
|
||||
|
||||
export type DeleteSchedulesEnabledParams = {
|
||||
id: string;
|
||||
companyId: string;
|
||||
sessionUser: string;
|
||||
};
|
||||
|
||||
export type EmployeeItem = {
|
||||
employeeId: string;
|
||||
};
|
||||
|
||||
export type EnableScheduleParams = {
|
||||
companyId: string;
|
||||
employees: EmployeeItem[];
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
sessionUser: string;
|
||||
};
|
||||
|
||||
export type DeleteSchedulesEnabledByCompanyParams = {
|
||||
companyId: string;
|
||||
};
|
||||
|
||||
export type DeleteSchedulesEnabledByEmployeeParams = {
|
||||
employeeId: string;
|
||||
};
|
||||
|
||||
export interface ISchedulesEnabled {
|
||||
id?: string;
|
||||
companyId: string;
|
||||
employeeId: string;
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
}
|
||||
|
||||
export interface SchedulesEnabledView {
|
||||
id: string;
|
||||
employeeId: string;
|
||||
employeeFullName: string;
|
||||
employeeEmail: string;
|
||||
employeeAvatar: string;
|
||||
employeeUserId: string;
|
||||
startDate: Date;
|
||||
endDate: Date;
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
export interface ISchedulesEnabledAdapter {
|
||||
create(data: CreateSchedulesEnabledParams): Promise<ISchedulesEnabled>;
|
||||
delete(id: string): Promise<void>;
|
||||
findOne(filters: FindSchedulesEnabledParams): Promise<ISchedulesEnabled | null>;
|
||||
}
|
||||
|
||||
export interface ISchedulesEnabledManager {
|
||||
schedulesEnabled: ISchedulesEnabledAdapter;
|
||||
createSchedulesEnabled(data: CreateSchedulesEnabledParams): Promise<ISchedulesEnabled>;
|
||||
enableSchedule(data: EnableScheduleParams): Promise<void>;
|
||||
disableSchedule(data: EnableScheduleParams): Promise<void>;
|
||||
find(data: FindSchedulesEnabledParams): Promise<SchedulesEnabledView[]>;
|
||||
findOne(data: FindSchedulesEnabledParams): Promise<ISchedulesEnabled | null>;
|
||||
delete(data: DeleteSchedulesEnabledParams): Promise<void>;
|
||||
exists(data: CheckSchedulesEnabledParams): Promise<boolean>;
|
||||
deleteSchedulesEnabledByCompany(data: DeleteSchedulesEnabledByCompanyParams): Promise<void>;
|
||||
deleteSchedulesEnabledByEmployee(data: DeleteSchedulesEnabledByEmployeeParams): Promise<void>;
|
||||
}
|
||||
@@ -0,0 +1,304 @@
|
||||
import { SchedulesEnabledAdapterMongoose } from "./SchedulesEnabled.Adapter.Mongoose";
|
||||
import {
|
||||
CheckSchedulesEnabledParams,
|
||||
CreateSchedulesEnabledParams,
|
||||
DeleteSchedulesEnabledByCompanyParams,
|
||||
DeleteSchedulesEnabledByEmployeeParams,
|
||||
DeleteSchedulesEnabledParams,
|
||||
EnableScheduleParams,
|
||||
FindSchedulesEnabledParams,
|
||||
ISchedulesEnabled,
|
||||
ISchedulesEnabledManager,
|
||||
SchedulesEnabledView,
|
||||
} from "./SchedulesEnabled.Interface";
|
||||
import { EmployeeRoles } from "../Employees/Employees.Interface";
|
||||
import UsersManager from "../Users/Users";
|
||||
import CompaniesManager from "../Companies/Companies";
|
||||
import EmployeesList from "../Employees/Employee";
|
||||
import { NoPermissionMessage } from "../../helpers/NoPermissionMessage";
|
||||
import { isNull } from "../../helpers/IsNull";
|
||||
import { joinStrings } from "../../helpers/String";
|
||||
import getAvatar from "../../helpers/getAvatar";
|
||||
import { NotificationsManager } from "../Notifications/Notifications";
|
||||
import dayjs from "dayjs";
|
||||
import { NotificationType } from "../../Models/SystemNotifications/SystemNotification.Interface";
|
||||
|
||||
dayjs.locale("es");
|
||||
|
||||
class SchedulesEnabledManager implements ISchedulesEnabledManager {
|
||||
schedulesEnabled: SchedulesEnabledAdapterMongoose;
|
||||
|
||||
constructor() {
|
||||
this.schedulesEnabled = new SchedulesEnabledAdapterMongoose();
|
||||
}
|
||||
|
||||
public async deleteSchedulesEnabledByEmployee(
|
||||
data: DeleteSchedulesEnabledByEmployeeParams
|
||||
): Promise<void> {
|
||||
await this.schedulesEnabled.schedulesEnabledList.deleteMany({
|
||||
employeeId: data.employeeId,
|
||||
});
|
||||
}
|
||||
|
||||
public async deleteSchedulesEnabledByCompany(data: DeleteSchedulesEnabledByCompanyParams): Promise<void> {
|
||||
await this.schedulesEnabled.schedulesEnabledList.deleteMany({ companyId: data.companyId });
|
||||
}
|
||||
|
||||
public async createSchedulesEnabled(data: CreateSchedulesEnabledParams): Promise<ISchedulesEnabled> {
|
||||
const sessionUser = await UsersManager.users.findOne({ _id: data.sessionUser });
|
||||
|
||||
if (!sessionUser) {
|
||||
throw new Error("No se ha encontrado el usuario o no tiene permisos para realizar esta acción");
|
||||
}
|
||||
|
||||
const companyCheck = await CompaniesManager.companies.findOne({ _id: data.companyId });
|
||||
|
||||
if (!companyCheck) {
|
||||
throw new Error("La compañia no existe");
|
||||
}
|
||||
|
||||
if (!(await EmployeesList.checkPermission(companyCheck, data.sessionUser, EmployeeRoles.ADMIN))) {
|
||||
throw new Error(NoPermissionMessage());
|
||||
}
|
||||
|
||||
const employeeCheck = await EmployeesList.employees.findOne({
|
||||
companyId: data.companyId,
|
||||
_id: data.employeeId,
|
||||
});
|
||||
|
||||
if (!employeeCheck) {
|
||||
throw new Error("El empleado no existe o no pertenece a la compañia.");
|
||||
}
|
||||
|
||||
const schedulesEnabledCheck = await this.schedulesEnabled.findOne({
|
||||
employeeId: data.employeeId,
|
||||
companyId: data.companyId,
|
||||
startDate: dayjs(data.startDate).startOf("day").toDate(),
|
||||
endDate: dayjs(data.endDate).endOf("day").toDate(),
|
||||
});
|
||||
|
||||
if (schedulesEnabledCheck) {
|
||||
return schedulesEnabledCheck;
|
||||
}
|
||||
|
||||
const newEmplyeesEnabled = await this.schedulesEnabled.create({
|
||||
employeeId: data.employeeId,
|
||||
companyId: data.companyId,
|
||||
startDate: dayjs(data.startDate).startOf("day").toDate(),
|
||||
endDate: dayjs(data.endDate).endOf("day").toDate(),
|
||||
sessionUser: data.sessionUser,
|
||||
});
|
||||
|
||||
const strFechaInicio = dayjs(data.startDate).format("DD/MM/YYYY");
|
||||
const strFechaFin = dayjs(data.endDate).format("DD/MM/YYYY");
|
||||
|
||||
await NotificationsManager.sendSystemNotification({
|
||||
userId: String(employeeCheck.userId),
|
||||
subject: "Se habilito un nuevo periodo de trabajo",
|
||||
message: `La organización ${companyCheck.name} ha habilitado las reservas para su usuario desde ${strFechaInicio} hasta ${strFechaFin}.`,
|
||||
type: NotificationType.SCHEDULE,
|
||||
code: String(employeeCheck._id)
|
||||
});
|
||||
|
||||
return newEmplyeesEnabled;
|
||||
}
|
||||
|
||||
public async enableSchedule(data: EnableScheduleParams): Promise<void> {
|
||||
const sessionUser = await UsersManager.users.findOne({ _id: data.sessionUser });
|
||||
|
||||
if (!sessionUser) {
|
||||
throw new Error("No se ha encontrado el usuario o no tiene permisos para realizar esta acción");
|
||||
}
|
||||
|
||||
const companyCheck = await CompaniesManager.companies.findOne({ _id: data.companyId });
|
||||
|
||||
if (!companyCheck) {
|
||||
throw new Error("La compañia no existe");
|
||||
}
|
||||
|
||||
if (!(await EmployeesList.checkPermission(companyCheck, data.sessionUser, EmployeeRoles.ADMIN))) {
|
||||
throw new Error(NoPermissionMessage());
|
||||
}
|
||||
|
||||
for (let i = 0; i < data.employees.length; i++) {
|
||||
const employeeId = data.employees[i].employeeId;
|
||||
const employeeCheck = await EmployeesList.employees.findOne({
|
||||
companyId: data.companyId,
|
||||
_id: employeeId,
|
||||
});
|
||||
|
||||
if (!employeeCheck) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const schedulesEnabledCheck = await this.schedulesEnabled.findOne({
|
||||
employeeId: employeeId,
|
||||
companyId: data.companyId,
|
||||
startDate: dayjs(data.startDate).startOf("day").toDate(),
|
||||
endDate: dayjs(data.endDate).endOf("day").toDate(),
|
||||
});
|
||||
|
||||
if (schedulesEnabledCheck) {
|
||||
continue;
|
||||
}
|
||||
|
||||
await this.schedulesEnabled.create({
|
||||
employeeId: employeeId,
|
||||
companyId: data.companyId,
|
||||
startDate: dayjs(data.startDate).startOf("day").toDate(),
|
||||
endDate: dayjs(data.endDate).endOf("day").toDate(),
|
||||
sessionUser: data.sessionUser,
|
||||
});
|
||||
|
||||
const strFechaInicio = dayjs(data.startDate).format("DD/MM/YYYY");
|
||||
const strFechaFin = dayjs(data.endDate).format("DD/MM/YYYY");
|
||||
|
||||
await NotificationsManager.sendSystemNotification({
|
||||
userId: String(employeeCheck.userId),
|
||||
subject: "Se habilito un nuevo periodo de trabajo",
|
||||
message: `La organización ${companyCheck.name} ha habilitado las reservas para su usuario desde ${strFechaInicio} hasta ${strFechaFin}.`,
|
||||
type: NotificationType.SCHEDULE,
|
||||
code: String(employeeCheck._id)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public async disableSchedule(data: EnableScheduleParams): Promise<void> {
|
||||
const sessionUser = await UsersManager.users.findOne({ _id: data.sessionUser });
|
||||
|
||||
if (!sessionUser) {
|
||||
throw new Error("No se ha encontrado el usuario o no tiene permisos para realizar esta acción");
|
||||
}
|
||||
|
||||
const companyCheck = await CompaniesManager.companies.findOne({ _id: data.companyId });
|
||||
|
||||
if (!companyCheck) {
|
||||
throw new Error("La compañia no existe");
|
||||
}
|
||||
|
||||
if (!(await EmployeesList.checkPermission(companyCheck, data.sessionUser, EmployeeRoles.ADMIN))) {
|
||||
throw new Error(NoPermissionMessage());
|
||||
}
|
||||
|
||||
for (let i = 0; i < data.employees.length; i++) {
|
||||
const employeeId = data.employees[i].employeeId;
|
||||
const employeeCheck = await EmployeesList.employees.findOne({
|
||||
companyId: data.companyId,
|
||||
_id: employeeId,
|
||||
});
|
||||
|
||||
if (!employeeCheck) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const schedulesEnabledCheck = await this.schedulesEnabled.findOne({
|
||||
employeeId: employeeId,
|
||||
companyId: data.companyId,
|
||||
startDate: dayjs(data.startDate).startOf("day").toDate(),
|
||||
endDate: dayjs(data.endDate).endOf("day").toDate(),
|
||||
});
|
||||
|
||||
if (schedulesEnabledCheck) {
|
||||
await this.schedulesEnabled.delete(String(schedulesEnabledCheck.id));
|
||||
const strFechaInicio = dayjs(data.startDate).format("DD/MM/YYYY");
|
||||
const strFechaFin = dayjs(data.endDate).format("DD/MM/YYYY");
|
||||
|
||||
await NotificationsManager.sendSystemNotification({
|
||||
userId: String(employeeCheck.userId),
|
||||
subject: "Se ha deshabilitado un periodo de trabajo",
|
||||
message: `La organización ${companyCheck.name} ha deshabilitado las reservas para su usuario desde ${strFechaInicio} hasta ${strFechaFin}.`,
|
||||
type: NotificationType.SCHEDULE,
|
||||
code: String(employeeCheck._id)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async exists(data: CheckSchedulesEnabledParams): Promise<boolean> {
|
||||
const scheduleCheck = await this.schedulesEnabled.schedulesEnabledList.findOne({
|
||||
employeeId: data.employeeId,
|
||||
companyId: data.companyId,
|
||||
startDate: { $lte: data.dateDay },
|
||||
endDate: { $gte: data.dateDay },
|
||||
});
|
||||
|
||||
if (scheduleCheck) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public async find(data: FindSchedulesEnabledParams): Promise<SchedulesEnabledView[]> {
|
||||
let employeeFilter: any = { companyId: data.companyId };
|
||||
if (data.employeeId) {
|
||||
employeeFilter = {
|
||||
...employeeFilter,
|
||||
employeeId: data.employeeId,
|
||||
};
|
||||
}
|
||||
const employees = await EmployeesList.employees.find(employeeFilter);
|
||||
|
||||
const returnData: SchedulesEnabledView[] = [];
|
||||
|
||||
for (const employee of employees) {
|
||||
const schedulesEnabled = await this.schedulesEnabled.schedulesEnabledList.findOne({
|
||||
employeeId: employee.id,
|
||||
companyId: data.companyId,
|
||||
startDate: dayjs(data.startDate).startOf("day").toDate(),
|
||||
endDate: dayjs(data.endDate).endOf("day").toDate(),
|
||||
});
|
||||
|
||||
const employUser = await UsersManager.users.findOne({
|
||||
_id: employee.userId,
|
||||
});
|
||||
|
||||
if (!employUser) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const fullName = joinStrings([employUser.firstName, employUser.lastName], " ");
|
||||
|
||||
returnData.push({
|
||||
id: schedulesEnabled ? schedulesEnabled.id : "",
|
||||
employeeId: isNull<string>(employee.id, ""),
|
||||
employeeFullName: fullName,
|
||||
employeeEmail: employUser.email,
|
||||
employeeAvatar: getAvatar(employUser.id, employUser.avatar, fullName),
|
||||
employeeUserId: isNull<string>(employee.userId, ""),
|
||||
startDate: data.startDate,
|
||||
endDate: data.endDate,
|
||||
enabled: schedulesEnabled ? true : false,
|
||||
});
|
||||
}
|
||||
|
||||
return returnData;
|
||||
}
|
||||
|
||||
public async findOne(data: FindSchedulesEnabledParams): Promise<ISchedulesEnabled | null> {
|
||||
return await this.schedulesEnabled.findOne(data);
|
||||
}
|
||||
|
||||
public async delete(data: DeleteSchedulesEnabledParams): Promise<void> {
|
||||
const sessionUser = await UsersManager.users.findOne({ _id: data.sessionUser });
|
||||
|
||||
if (!sessionUser) {
|
||||
throw new Error("No se ha encontrado el usuario o no tiene permisos para realizar esta acción");
|
||||
}
|
||||
|
||||
const companyCheck = await CompaniesManager.companies.findOne({ _id: data.companyId });
|
||||
|
||||
if (!companyCheck) {
|
||||
throw new Error("La compañia no existe");
|
||||
}
|
||||
|
||||
if (!(await EmployeesList.checkPermission(companyCheck, data.sessionUser, EmployeeRoles.ADMIN))) {
|
||||
throw new Error(NoPermissionMessage());
|
||||
}
|
||||
|
||||
await this.schedulesEnabled.delete(String(data.id));
|
||||
}
|
||||
}
|
||||
|
||||
const SchedulesEnabledList = new SchedulesEnabledManager();
|
||||
|
||||
export default SchedulesEnabledList;
|
||||
Reference in New Issue
Block a user