All files / src/routes service.route.ts

100% Statements 58/58
100% Branches 46/46
100% Functions 12/12
100% Lines 53/53

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131                                              1x 1x 1x   1x   1x 4x   4x 4x 2x   2x 2x         10x   10x 2x       8x 8x 2x   6x 6x 4x       2x         71x   71x   2x   69x           69x 4x   65x     8x   8x 2x   6x 6x 2x   4x     10x   10x 2x   8x 2x   6x           6x 2x   4x     8x 8x 2x   6x 6x 2x   4x 4x             1x
/*
MIT License
 
Copyright (c) 2021 Mandar Patil (mandarons@pm.me)
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import express from 'express';
import * as serviceModel from '../db/services.schema';
import utils from './route.utils';
 
const Router = express.Router();
 
Router.get('/all',
    async (req: express.Request, res: express.Response) => {
        // Return list of services
        const fromDB = await serviceModel.getAllServices();
        if (!fromDB.success) {
            return utils.errorResponse(res, 500, fromDB.errorMessage as string);
        }
        const services = fromDB.values as serviceModel.IServiceRecordAttributes[];
        return utils.successResponse(res, `${services.length} services found.`, {
            services
        });
    });
 
Router.get('/', async (req, res) => {
    // Get an existing service
    if ('' === req.body.shortName || req.body.shortName === undefined) {
        return utils.errorResponse(res, 400, 'Missing data.', {
            data: req.body
        });
    }
    const fromDB = await serviceModel.findService(req.body.shortName);
    if (!fromDB.success) {
        return utils.errorResponse(res, 500, fromDB.errorMessage as string);
    }
    const service = fromDB.values as serviceModel.IServiceRecordAttributes;
    if (service !== undefined) {
        return utils.successResponse(res, 'Service found.', {
            service
        });
    }
    return utils.errorResponse(res, 404, 'Service not found.', {
        data: req.body
    });
});
 
Router.post('/', async (req, res) => {
    // Post a new service
    if (req.body.name === undefined || req.body.shortName === undefined || req.body.logoURL === undefined || req.body.url === undefined ||
        req.body.name === '' || req.body.shortName === '' || req.body.logoURL === '' || req.body.url === '' || !utils.isValidService(req.body)) {
        return utils.errorResponse(res, 400, 'Missing data.', { ...req.body });
    }
    const fromDB = await serviceModel.addService({
        name: req.body.name,
        shortName: req.body.shortName,
        url: req.body.url,
        logoURL: req.body.logoURL
    } as serviceModel.IServiceRecordAttributes);
    if (!fromDB.success) {
        return utils.errorResponse(res, 500, fromDB.errorMessage as string);
    }
    return utils.successResponse(res, `Service ${req.body.name} has been added.`);
});
 
Router.delete('/', async (req, res) => {
    // Delete an existing service
    if (req.body.shortName === '' || req.body.shortName === undefined) {
        return utils.errorResponse(res, 400, 'Missing required data.', { ...req.body });
    }
    const fromDB = await serviceModel.deleteService(req.body.shortName);
    if (!fromDB.success) {
        return utils.errorResponse(res, 500, fromDB.errorMessage as string);
    }
    return utils.successResponse(res, `Service ${req.body.shortName} has been removed.`, { ...fromDB.data });
});
 
Router.put('/', async (req, res) => {
    // Update an existing service
    if (false === utils.isValidService(req.body.service) || undefined === req.body.shortName || '' === req.body.shortName) {
        return utils.errorResponse(res, 400, 'Missing required data.', { ...req.body });
    }
    if (req.body.shortName !== req.body.service.shortName) {
        return utils.errorResponse(res, 403, 'shortName cannot be changed.', { ...req.body });
    }
    const fromDB = await serviceModel.updateService({
        name: req.body.service.name,
        shortName: req.body.service.shortName,
        url: req.body.service.url,
        logoURL: req.body.service.logoURL
    });
    if (!fromDB.success) {
        return utils.errorResponse(res, 500, fromDB.errorMessage as string);
    }
    return utils.successResponse(res, `Service ${req.body.shortName} has been updated.`, { ...fromDB.data });
});
 
Router.get('/status', async (req, res) => {
    if (req.query.shortName === undefined || req.query.shortName === '') {
        return utils.errorResponse(res, 403, 'Missing or incorrect data.', { ...req.query });
    }
    const fromDB = await serviceModel.getStatus(req.query.shortName as string);
    if (!fromDB.success) {
        return utils.errorResponse(res, 500, fromDB.errorMessage as string);
    };
    const data = (fromDB.data as { [key: string]: boolean | Date | null; })!;
    return utils.successResponse(res, req.query.shortName + (data.lastOnline ? ' was online.' : ' was never online.'), {
        lastOnline: data.lastOnline,
        online: data.online
    });
});
 
 
export default Router;