import { LIMIT_PER_PROJECT } from "../../Types/Database/LimitMax";
import DatabaseService from "./DatabaseService";
import Model from "../../Models/DatabaseModels/OnCallDutyPolicyTimeLog";
export class Service extends DatabaseService {
    constructor() {
        super(Model);
    }
    async startTimeLogForUser(data) {
        const { onCallDutyPolicyId, userId, teamId, onCallDutyPolicyScheduleId, startsAt, } = data;
        // check if the time log already exists for the user.
        const existingTimeLog = await this.findOneBy({
            query: Object.assign(Object.assign({ projectId: data.projectId, onCallDutyPolicyId, onCallDutyPolicyEscalationRuleId: data.onCallDutyPolicyEscalationRuleId, userId }, (teamId && { teamId })), (onCallDutyPolicyScheduleId && { onCallDutyPolicyScheduleId })),
            props: {
                isRoot: true,
            },
        });
        if (existingTimeLog) {
            return existingTimeLog;
        }
        const timeLog = new Model();
        timeLog.onCallDutyPolicyId = onCallDutyPolicyId;
        timeLog.userId = userId;
        if (teamId) {
            timeLog.teamId = teamId;
        }
        timeLog.onCallDutyPolicyEscalationRuleId =
            data.onCallDutyPolicyEscalationRuleId;
        if (onCallDutyPolicyScheduleId) {
            timeLog.onCallDutyPolicyScheduleId = onCallDutyPolicyScheduleId;
        }
        timeLog.projectId = data.projectId;
        timeLog.startsAt = startsAt;
        return await this.create({
            data: timeLog,
            props: {
                isRoot: true,
            },
        });
    }
    async endTimeForSchedule(data) {
        const { endsAt, onCallDutyPolicyScheduleId } = data;
        await this.updateBy({
            query: {
                projectId: data.projectId,
                onCallDutyPolicyScheduleId,
            },
            data: {
                endsAt: endsAt,
            },
            limit: LIMIT_PER_PROJECT,
            skip: 0,
            props: {
                isRoot: true,
            },
        });
    }
    async endTimeForTeam(data) {
        const { endsAt, teamId } = data;
        await this.updateBy({
            query: {
                projectId: data.projectId,
                teamId,
            },
            limit: LIMIT_PER_PROJECT,
            skip: 0,
            data: {
                endsAt: endsAt,
            },
            props: {
                isRoot: true,
            },
        });
    }
    async endTimeForUser(data) {
        const { endsAt, userId } = data;
        await this.updateBy({
            query: {
                projectId: data.projectId,
                userId,
            },
            limit: LIMIT_PER_PROJECT,
            skip: 0,
            data: {
                endsAt: endsAt,
            },
            props: {
                isRoot: true,
            },
        });
    }
    async endTimeLogForUser(data) {
        const { onCallDutyPolicyId, userId, teamId, onCallDutyPolicyScheduleId, endsAt, } = data;
        await this.updateBy({
            query: Object.assign(Object.assign({ projectId: data.projectId, onCallDutyPolicyId, onCallDutyPolicyEscalationRuleId: data.onCallDutyPolicyEscalationRuleId, userId }, (teamId && { teamId })), (onCallDutyPolicyScheduleId && { onCallDutyPolicyScheduleId })),
            limit: LIMIT_PER_PROJECT,
            skip: 0,
            data: {
                endsAt: endsAt,
            },
            props: {
                isRoot: true,
            },
        });
    }
}
export default new Service();
//# sourceMappingURL=OnCallDutyPolicyTimeLogService.js.map