{"size": 4987, "ext": "js", "max_stars_repo_path": "frontend/src/api/mockAPI/wage_chunks.js", "max_stars_repo_name": "siefkenj/tapp", "max_stars_repo_stars_event_min_datetime": "2018-09-28T00:13:08.000Z", "max_stars_repo_stars_event_max_datetime": "2021-02-12T16:45:54.000Z", "max_issues_repo_path": "frontend/src/api/mockAPI/wage_chunks.js", "max_issues_repo_name": "siefkenj/tapp", "max_issues_repo_issues_event_min_datetime": "2017-05-29T15:39:11.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-04T17:11:07.000Z", "max_forks_repo_path": "frontend/src/api/mockAPI/wage_chunks.js", "max_forks_repo_name": "siefkenj/tapp", "max_forks_repo_forks_event_min_datetime": "2017-05-24T13:37:08.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-20T17:52:21.000Z"}
{"max_stars_count": 10, "max_issues_count": 437, "max_forks_count": 33, "avg_line_length": 36.1376811594, "max_line_length": 87, "alphanum_fraction": 0.5542410267}
import { documentCallback, docApiPropTypes } from "../defs/doc-generation";
import { find, MockAPIController, findAllById } from "./utils";
import { Assignment } from "./assignments";
import { Position } from "./positions";

export class WageChunk extends MockAPIController {
    constructor(data) {
        super(data, data.wage_chunks);
    }
    validateNew() {
        // There is nothing to validate for a wage chunk
    }
    findAllByAssignment(assignment) {
        const matchingAssignment = new Assignment(this.data).rawFind(
            assignment
        );
        if (!matchingAssignment) {
            throw new Error(
                `Could not find assignment matching ${JSON.stringify(
                    assignment
                )}`
            );
        }
        // Make sure we apply `this.find` so that the rates are computed.
        return findAllById(
            [matchingAssignment.id],
            this.ownData,
            "assignment_id"
        ).map((x) => this.find(x));
    }
    find(wageChunk) {
        const rawWageChunk = this.rawFind(wageChunk);
        const ret = { ...rawWageChunk };
        if (ret.rate == null) {
            // If the rate is not set, look it up from the session
            const session = this.getSession(wageChunk);
            if (session.rate2 == null) {
                ret.rate = session.rate1;
            } else {
                // Rates switch from session.rate1 to session.rate2 on January 1 of
                // the year following the session start date.
                const start_date = new Date(wageChunk.start_date);
                const end_date = new Date(wageChunk.end_date);
                const session_start_date = new Date(session.start_date);
                // For `Date`, 11 is december
                const december = new Date(
                    session_start_date.getFullYear(),
                    11,
                    31
                );
                if (start_date <= december && end_date <= december) {
                    ret.rate = session.rate1;
                } else {
                    ret.rate = session.rate2;
                }
            }
        }
        return ret;
    }
    getSession(wageChunk) {
        const rawWageChunk = this.rawFind(wageChunk);
        const assignment = new Assignment(this.data).rawFind({
            id: rawWageChunk.assignment_id,
        });
        const position = new Assignment(this.data).getPosition(assignment);
        return new Position(this.data).getSession(position);
    }
    upsertByAssignment(wageChunk, assignment) {
        const matchingAssignment = new Assignment(this.data).rawFind(
            assignment
        );
        if (!matchingAssignment) {
            throw new Error(
                `Could not find assignment matching ${JSON.stringify(
                    assignment
                )}`
            );
        }
        return this.upsert({
            ...wageChunk,
            assignment_id: matchingAssignment.id,
        });
    }
    /**
     * Sets the list of wage chunks associated with a given assignment to `wageChunks`.
     * Any wage chunks omitted from the list are deleted.
     *
     * @param {*} wageChunks
     * @param {*} assignment
     * @returns
     * @memberof WageChunk
     */
    setAllByAssignment(wageChunks, assignment) {
        const matchingAssignment = new Assignment(this.data).rawFind(
            assignment
        );
        if (!matchingAssignment) {
            throw new Error(
                `Could not find assignment matching ${JSON.stringify(
                    assignment
                )}`
            );
        }
        const existingWageChunks = this.findAllByAssignment(assignment);
        // Now we can upsert all the wageChunks
        wageChunks.forEach((chunk) =>
            this.upsertByAssignment(chunk, matchingAssignment)
        );
        // every wage chunk that is not in the `wageChunks` list gets deleted
        for (const wageChunk of existingWageChunks) {
            if (!find(wageChunk, wageChunks)) {
                this.delete(wageChunk);
            }
        }
        // Recompute the wage chunks list. This is "needless"
        // logically, but it might catch some bugs at some point.
        return this.findAllByAssignment(matchingAssignment);
    }
}

export const wageChunkRoutes = {
    get: {},
    post: {
        "/wage_chunks": documentCallback({
            func: (data, params, body) => new WageChunk(data).upsert(body),
            posts: docApiPropTypes.wageChunk,
            summary: "Upsert a wage_chunk",
            returns: docApiPropTypes.wageChunk,
        }),
        "/wage_chunks/delete": documentCallback({
            func: (data, params, body) => new WageChunk(data).delete(body),
            posts: docApiPropTypes.wageChunk,
            summary: "Delete a wage_chunk. Must have a valid id specified.",
            returns: docApiPropTypes.wageChunk,
        }),
    },
};
