// @ts-check
/**
* @import {ExpressRequestAuthorized, ExpressResponse} from '../../types.js'
*/
import * as locationService from './service.js';
import { errorLoggerUpdate, errorLoggerRead, requestUpdateLogger, readLogger } from '../../utils/requestLogger.js';
import { UUID2hex } from '@commtool/sql-query';
import { paginateList } from '../../utils/paginateList.js';
/**
* Controller for creating or updating a location
* @param {ExpressRequestAuthorized} req
* @param {ExpressResponse} res
*/
export const createOrUpdateLocationController = async (req, res) => {
try {
const result = await locationService.createOrUpdateLocation(
req.session,
req.params.group,
req.body,
req.query
);
if (!result.success) {
res.status(300).json(result);
} else {
res.json(result);
}
} catch (e) {
errorLoggerUpdate({ error: e });
res.status(500).json({ success: false, message: e.message });
}
};
/**
* Controller for deleting a location
* @param {ExpressRequestAuthorized} req
* @param {ExpressResponse} res
*/
export const deleteLocationController = async (req, res) => {
try {
const UID = UUID2hex(req.params.UID);
const result = await locationService.deleteLocation(UID);
res.json(result);
} catch (e) {
errorLoggerUpdate({ error: e });
res.status(500).json({ success: false, message: e.message });
}
};
/**
* Controller for getting a single location
* @param {ExpressRequestAuthorized} req
* @param {ExpressResponse} res
*/
export const getLocationController = async (req, res) => {
try {
const UID = UUID2hex(req.params.UID);
const result = await locationService.getLocation(UID);
res.json(result);
} catch (e) {
errorLoggerRead({ error: e });
res.status(500).json({ success: false, message: e.message });
}
};
/**
* Controller for getting location listing (with pagination support)
* @param {ExpressRequestAuthorized} req
* @param {ExpressResponse} res
* @param {Function} next
*/
export const getLocationListingController = async (req, res, next) => {
try {
// if we want the data paginated- the user has supplied the __page parameter
if (req.query.__page) {
// paginated api request
const result = await paginateList(req, async (req) => {
return await locationService.getListing(req.session, req.query);
});
res.json(result);
} else {
// normal api request
const result = await locationService.getListing(req.session, req.query);
res.json({ success: true, result });
}
} catch (e) {
errorLoggerRead({ error: e });
res.status(500).json({ success: false, message: e.message });
}
};