All files / daos ratings.ts

83.33% Statements 15/18
50% Branches 2/4
90% Functions 9/10
86.67% Lines 13/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88          1x                     1x       1x                 1x   1x 1x                                             1x 1x   1x               1x       1x                       1x 1x        
import { MyModels } from "../models";
import {literal, Sequelize, Transaction} from "sequelize";
import Rating from "../types/Rating";
import { RatingAttributes } from "../models/Ratings";
import Position, { GeoJSONPoint, PositionParams } from "../types/Position";
import { ConflictError, InvalidKeyError } from "../types/errors";
import {logger} from "../server";
/**
 * Add a dumpster to the database table.
 * When adding, the dumpster does not yet have an ID.
 * A rather complex routine since a revision must be created,
 * and the dumpster's current revision must be set to the one that was added.
 *
 * @param dumpster
 * @return The newly posted data, with an ID
 */
export default function ({
                             Ratings,
                             sequelize,
                         }: MyModels) {
    return {
        /**
         * Adds a rating to a dumpster, returning the inserted entity
         *
         * @param dumpsterID
         * @param rating
         * @param userID
         */
 
        addOne: async (dumpsterID : number, rating : number, userID : number ) => {
            // Perform transaction
            return await sequelize.transaction(async t => {
                return await Ratings.create(
                    {
                        dumpsterID,
                        rating,
                        userID
                    },
                    {transaction: t},
                ).catch(_ => {
                    throw new ConflictError(
                        "A rating already exists for this dumpster by this user",
                    );
                });
 
            });
        },
        /**
         * update a rating to a dumpster, returning the inserted entity
         *
         * @param userID
         * @param dumpsterID
         * @param rating
         */
 
        updateOne: async (userID : number, dumpsterID : number, rating : number) => {
            return sequelize.transaction(async t => {
                // Check that the pair of revision and dumpster ID is valid
                const match = await Ratings.findOne({
                    where: {
                        userID,
                        dumpsterID,
                    },
                    transaction: t,
                });
 
                Iif (!match)
                    throw new InvalidKeyError(
                        "There is no rating for this dumpster from this user",
                    );
                const updatedRating = await Ratings.update(
                    {
                        rating,
                    },
                    {
                        where: {
                            userID,
                            dumpsterID,
                        },
                        transaction: t,
                    },
                );
                Iif(!updatedRating) return false;
                else return true;
            });
        },
    }
}