<commit_before>// FacilityAssignServices ----------------------------------------------------

// Services implementation for Facility -> Assign models.

// External Modules ----------------------------------------------------------

// Internal Modules ----------------------------------------------------------

import Assign from "../models/Assign";
import Checkin from "../models/Checkin";
import Facility from "../models/Facility";
import Guest from "../models/Guest";
import { toDateObject } from "../util/dates";
import { BadRequest, NotFound, ServerError } from "../util/http-errors";

// Public Objects ------------------------------------------------------------

class FacilityAssignServices {

    public async assignsAssign
        (facilityId: number, checkinId: number, assign: Assign): Promise<Checkin> {

        // Validate specified facilityId
        if (!assign.facilityId) {
            throw new BadRequest(
                `assign: Missing facilityId in assign data`,
                "FacilityServices.assignsAssign()");
        } else {
            if (assign.facilityId !== facilityId) {
                throw new BadRequest(
                    `assign: facilityId is ${facilityId} but assign data says ${assign.facilityId}`,
                    "FacilityServices.assignsAssign()");
            }
        }
        const facility = await Facility.findByPk(facilityId);
        if (!facility) {
            throw new NotFound(
                `facilityId: Missing Facility ${facilityId}`,
                "FacilityServices.assignsAssign()");
        }

        // Validate specified guestId
        if (!assign.guestId) {
            throw new BadRequest(
                `assign: Missing guestId in assign data`,
                "FacilityServices.assignsAssign()");
        }
        const guest = await Guest.findByPk(assign.guestId);
        if (!guest) {
            throw new BadRequest(
                `assign: Missing Guest ${assign.guestId}`,
                "FacilityServices.assignsAssign()");
        }
        if (guest.facilityId !== facilityId) {
            throw new BadRequest(
                `guestId: Guest ${guest.firstName} ${guest.lastName}`
                + `does not belong to Facility ${facility.name}`,
                "FacilityServices.assignsAssign()");
        }

        // Validate specified checkinId
        const checkin = await Checkin.findByPk(checkinId);
        if (!checkin) {
            throw new NotFound(
                `checkinId: Missing Checkin ${checkinId}`,
                "FacilityServices.assignsAssign()");
        }
        if (checkin.facilityId !== facility.id) {
            throw new BadRequest(
                `checkinId: Checkin ${checkinId} does not belong to Facility ${facilityId}`,
                "FacilityServices.assignsAssign()");
        }

        // For an assigned Checkin, must be assigned to the specified guestId
        // (allows for information updates)
        if (checkin.guestId && (checkin.guestId !== assign.guestId)) {
            throw new BadRequest(
                `checkinId: Checkin ${checkinId} is already assigned to Guest ${checkin.guestId}`,
                "FacilityServices.assignsAssign()");
        }

        // For an unassigned Checkin, verify that this guestId is not checked in elsewhere
        if (!checkin.guestId) {
            const options = {
                where: {
                    checkinDate: checkin.checkinDate,
                    facilityId: checkin.facilityId,
                    guestId: assign.guestId,
                }
            }
            const results = await Checkin.findAll(options);
            if (results.length > 0) {
                throw new BadRequest(
                    `guestId: Guest ${guest.firstName} ${guest.lastName}`
                    + ` is already assigned to mat ${results[0].matNumber}`,
                    "FacilityServices.assignsAssign()");
            }
        }

        // Perform the assignment changes
        checkin.comments = assign.comments ? assign.comments : undefined;
        checkin.guestId = assign.guestId;
        checkin.paymentAmount = assign.paymentAmount ? assign.paymentAmount : undefined;
        checkin.paymentType = assign.paymentType ? assign.paymentType : undefined;
        checkin.showerTime = assign.showerTime ? toDateObject(assign.showerTime) : undefined;
        checkin.wakeupTime = assign.wakeupTime ? toDateObject(assign.wakeupTime) : undefined;

        // Persist and return these changes
        // WARNING:  IN-PLACE UPDATES OF SEQUELIZE MODELS ARE REALLY FUNKY!!!
        // WARNING:  Use "fields" to choose which columns to update
        // WARNING:  Use "returning: true" to return the updated row (Postgres specific!)
        // WARNING:  Use "validating: false" to avoid validating things you didn't include
        const [count, results] = await Checkin.update(assign, {
            fields: [
                "comments", "guestId", "paymentAmount", "paymentType",
                "showerTime", "wakeupTime"<commit_msg>Switch assignsAssign() to the partial update style.<commit_after>// FacilityAssignServices ----------------------------------------------------

// Services implementation for Facility -> Assign models.

// External Modules ----------------------------------------------------------

// Internal Modules ----------------------------------------------------------

import Assign from "../models/Assign";
import Checkin from "../models/Checkin";
import Facility from "../models/Facility";
import Guest from "../models/Guest";
import { toDateObject } from "../util/dates";
import { BadRequest, NotFound, ServerError } from "../util/http-errors";

// Public Objects ------------------------------------------------------------

class FacilityAssignServices {

    public async assignsAssign
        (facilityId: number, checkinId: number, assign: Assign): Promise<Checkin> {

        // Validate specified facilityId
        if (!assign.facilityId) {
            throw new BadRequest(
                `assign: Missing facilityId in assign data`,
                "FacilityServices.assignsAssign()");
        } else {
            if (assign.facilityId !== facilityId) {
                throw new BadRequest(
                    `assign: facilityId is ${facilityId} but assign data says ${assign.facilityId}`,
                    "FacilityServices.assignsAssign()");
            }
        }
        const facility = await Facility.findByPk(facilityId);
        if (!facility) {
            throw new NotFound(
                `facilityId: Missing Facility ${facilityId}`,
                "FacilityServices.assignsAssign()");
        }

        // Validate specified guestId
        if (!assign.guestId) {
            throw new BadRequest(
                `assign: Missing guestId in assign data`,
                "FacilityServices.assignsAssign()");
        }
        const guest = await Guest.findByPk(assign.guestId);
        if (!guest) {
            throw new BadRequest(
                `assign: Missing Guest ${assign.guestId}`,
                "FacilityServices.assignsAssign()");
        }
        if (guest.facilityId !== facilityId) {
            throw new BadRequest(
                `guestId: Guest ${guest.firstName} ${guest.lastName}`
                + `does not belong to Facility ${facility.name}`,
                "FacilityServices.assignsAssign()");
        }

        // Validate specified checkinId
        const checkin = await Checkin.findByPk(checkinId);
        if (!checkin) {
            throw new NotFound(
                `checkinId: Missing Checkin ${checkinId}`,
                "FacilityServices.assignsAssign()");
        }
        if (checkin.facilityId !== facility.id) {
            throw new BadRequest(
                `checkinId: Checkin ${checkinId} does not belong to Facility ${facilityId}`,
                "FacilityServices.assignsAssign()");
        }

        // For an assigned Checkin, must be assigned to the specified guestId
        // (allows for information updates)
        if (checkin.guestId && (checkin.guestId !== assign.guestId)) {
            throw new BadRequest(
                `checkinId: Checkin ${checkinId} is already assigned to Guest ${checkin.guestId}`,
                "FacilityServices.assignsAssign()");
        }

        // For an unassigned Checkin, verify that this guestId is not checked in elsewhere
        if (!checkin.guestId) {
            const options = {
                where: {
                    checkinDate: checkin.checkinDate,
                    facilityId: checkin.facilityId,
                    guestId: assign.guestId,
                }
            }
            const results = await Checkin.findAll(options);
            if (results.length > 0) {
                throw new BadRequest(
                    `guestId: Guest ${guest.firstName} ${guest.lastName}`
                    + ` is already assigned to mat ${results[0].matNumber}`,
                    "FacilityServices.assignsAssign()");
            }
        }

        // Prepare the deassignment changes
        const updates = {
            comments: assign.comments,
            guestId: assign.guestId,
            paymentAmount: assign.paymentAmount,
            paymentType: assign.paymentType,
            showerTime: assign.showerTime,
            wakeupTime: assign.wakeupTime,
        }

        // Persist and return these changes
        // WARNING:  IN-PLACE UPDATES OF SEQUELIZE MODELS ARE REALLY FUNKY!!!
        // WARNING:  Use "fields" to choose which columns to update
        // WARNING:  Use "returning: true" to return the updated row (Postgres specific!)
        // WARNING:  Use "validating: false" to avoid validating things you didn't include
        const [count, results] = await Checkin.update(updates, {
            fields: [
                "comments", "guestId", "paymentAmount", "paymentType",
                "showerTime", "wakeupTime"
{'fraction_non_alphanumeric': 0.14259945886361358, 'fraction_numerical': 0.0004008417677121956, 'mean_word_length': 1.6885775862068966, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '511142', 'n_tokens_mistral': 2562, 'n_tokens_neox': 2441, 'n_words': 671}