How to use the api/controllers/utils/catchErrors function in api

To help you get started, we’ve selected a few api examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github LearningLocker / learninglocker / api / src / controllers / PersonaController.js View on Github external
organisation: getOrgFromAuthInfo(authInfo),
    personaId,
    name: newName
  });

  // Updates the name
  await reasignPersonaStatements({
    organisation: getOrgFromAuthInfo(authInfo),
    toId: personaId,
    fromId: personaId
  });

  return entityResponse(res, persona);
});

const mergePersona = catchErrors(async (req, res) => {
  const authInfo = getAuthFromRequest(req);

  await getScopeFilter({
    modelName: MODEL_NAME,
    actionName: 'edit',
    authInfo
  });

  const result = await personaService.mergePersona({
    organisation: getOrgFromAuthInfo(authInfo),
    fromPersonaId: req.query.mergePersonaFromId,
    toPersonaId: req.query.mergePersonaToId
  });

  await reasignPersonaStatements({
    fromId: req.query.mergePersonaFromId,
github LearningLocker / learninglocker / api / src / controllers / PersonaIdentifierController.js View on Github external
await getScopeFilter({
    modelName: MODEL_NAME,
    actionName: 'view',
    authInfo
  });

  const { identifier } = await personaService.getIdentifier({
    organisation: getOrgFromAuthInfo(authInfo),
    id: req.params.personaIdentifierId
  });

  return entityResponse(res, identifier);
});

const getPersonaIdentifiers = catchErrors(async (req, res) => {
  const authInfo = getAuthFromRequest(req);

  await getScopeFilter({
    modelName: MODEL_NAME,
    actionName: 'view',
    authInfo
  });

  const sort = getJSONFromQuery(req, 'sort', { _id: 1 });
  const limit = getFromQuery(req, 'limit', undefined, parseInt);
  const skip = getFromQuery(req, 'skip', undefined, parseInt);

  const query = getJSONFromQuery(req, 'query', {});
  const filter = getJSONFromQuery(req, 'filter', query);

  const {
github LearningLocker / learninglocker / api / src / controllers / StatementMetadataController.js View on Github external
{ _id: objectId(req.params.id) },
      scopeFilter
    ]
  };

  const metadata = mapDot(req.body, encodeDot);
  await Statement.updateOne(filter, {
    $set: mapKeys(metadata, (_value, key) => `metadata.${key}`)
  });
  const model = await Statement.findOne(filter).select({ _id: 1, organisation: 1, metadata: 1 }).lean();
  generateQueryBuilderCaches({ metadata }, model.organisation);

  return res.status(200).send(mapDot({ _id: model._id, metadata: model.metadata }));
});

export const postStatementMetadata = catchErrors(async (req, res) => {
  const authInfo = getAuthFromRequest(req);

  const scopeFilter = await getScopeFilter({
    modelName: 'statement',
    actionName: 'edit',
    authInfo
  });

  const filter = {
    $and: [
      { _id: objectId(req.params.id) },
      scopeFilter
    ]
  };
  const metadata = mapDot(req.body, encodeDot);
github LearningLocker / learninglocker / api / src / controllers / PersonaIdentifierController.js View on Github external
import asignIdentifierToStatements from 'lib/services/persona/asignIdentifierToStatements';
import identifierHasStatements from 'lib/services/persona/identifierHasStatements';
import getPersonaService from 'lib/connections/personaService';
import {
  isUndefined,
  omitBy
} from 'lodash';
import { entityResponse, entitiesResponse } from 'api/controllers/utils/entitiesResponse';
import setupIfi from 'lib/services/persona/setupIfi';
import validateIfi from 'lib/services/persona/validateIfi';

const MODEL_NAME = 'personaIdentifier';

const personaService = getPersonaService();

const personaIdentifierConnection = catchErrors(async (req, res) => {
  const { before, after } = req.query;

  const sort = getJSONFromQuery(req, 'sort', { _id: 1 });
  const hint = getJSONFromQuery(req, 'hint', undefined);
  const project = getJSONFromQuery(req, 'project', undefined);
  const first = getFromQuery(req, 'first', undefined, parseInt);
  const last = getFromQuery(req, 'last', undefined, parseInt);
  const authInfo = getAuthFromRequest(req);

  const scopeFilter = await getScopeFilter({
    modelName: MODEL_NAME,
    actionName: 'view',
    authInfo
  });

  const reqFilter = getJSONFromQuery(req, 'filter', {});
github LearningLocker / learninglocker / api / src / controllers / PersonaIdentifierController.js View on Github external
sort,
    limit,
    skip,
    persona,
    filter: inFilter,
    organisation: getOrgFromAuthInfo(authInfo),
  });

  return entitiesResponse(res, identifiers);
});

/**
 * Update the personaIdentifier
 * Only the persona can be changed via this method
 */
const updatePersonaIdentifier = catchErrors(async (req, res) => {
  const authInfo = getAuthFromRequest(req);

  await getScopeFilter({
    modelName: MODEL_NAME,
    actionName: 'edit',
    authInfo
  });

  const { persona } = req.body;
  if (!persona) {
    throw new ClientError('`persona` must be included when updating an identifier');
  }

  const organisation = getOrgFromAuthInfo(authInfo);
  const { identifier } = await personaService.setIdentifierPersona({
    organisation,
github LearningLocker / learninglocker / api / src / controllers / PersonaAttributeController.js View on Github external
await getScopeFilter({
    modelName: MODEL_NAME,
    actionName: 'view',
    authInfo
  });

  const { attribute } = await personaService.getAttribute({
    organisation: getOrgFromAuthInfo(authInfo),
    id: req.params.personaAttributeId
  });

  return entityResponse(res, attribute);
});

const getPersonaAttributes = catchErrors(async (req, res) => {
  const authInfo = getAuthFromRequest(req);

  await getScopeFilter({
    modelName: MODEL_NAME,
    actionName: 'view',
    authInfo
  });

  const sort = getJSONFromQuery(req, 'sort', { _id: 1 });
  const limit = getFromQuery(req, 'limit', undefined, parseInt);
  const skip = getFromQuery(req, 'skip', undefined, parseInt);

  const query = getJSONFromQuery(req, 'query', {});
  const filter = getJSONFromQuery(req, 'filter', query);

  const {
github LearningLocker / learninglocker / api / src / controllers / StatementController.js View on Github external
const maxTimeMS = Number(req.query.maxTimeMS) || MAX_TIME_MS;
  const maxScan = Number(req.query.maxScan) || MAX_SCAN;
  const pipeline = JSON.parse(req.query.pipeline);
  const out = statementsService.aggregate({
    authInfo,
    limit,
    skip,
    cache,
    maxTimeMS,
    maxScan,
    pipeline
  });
  return out;
};

const aggregateStatements = catchErrors(async (req, res) => {
  const results = await aggregate(req);
  res.set('Content-Type', 'application/json');
  res.write(results);
  return res.end();
});

const aggregateStatementsV1 = catchErrors(async (req, res) => {
  const results = await aggregate(req);
  const strRes = `{ "waitedMS": 0, "result": ${results}, "ok": 1 }`;
  res.set('Content-Type', 'application/json');
  res.write(strRes);
  return res.end();
});

const countStatements = catchErrors(async (req, res) => {
  const filter = getJSONFromQuery(
github LearningLocker / learninglocker / api / src / controllers / ImportPersonasController.js View on Github external
const importPersonas = catchErrors(async (req, res) => {
  const authInfo = getAuthFromRequest(req);

  const { id } = req.body;

  const personaService = getPersonaService();
  const { personaImport } = await importPersonasService({
    id,
    authInfo,
    personaService,
  });

  return res.status(200).json(personaImport);
});

const importPersonasError = catchErrors(async (req, res) => {
  const authInfo = getAuthFromRequest(req);

  const id = req.params.id;

  const filter = await getScopeFilter({
    modelName: 'personasImport',
    actionName: 'view',
    authInfo
  });

  const personaImport = await PersonasImport.findOne({
    _id: objectId(id),
    ...filter
  });

  const csvHandle = personaImport.csvErrorHandle || personaImport.csvHandle;
github LearningLocker / learninglocker / api / src / controllers / AuthController.js View on Github external
([accessToken, refreshToken]) =>
            res
              .cookie(
                `refresh_token_user_${user._id}`,
                refreshToken,
                buildRefreshCookieOption(req.protocol),
              )
              .set('Content-Type', 'text/plain')
              .send(accessToken)
          )
        .catch(authFailure)
    );
  })(req, res, next);
};

const jwtRefresh = catchErrors(async (req, res) => {
  try {
    const refreshToken = req.cookies[`refresh_token_${req.body.tokenType}_${req.body.id}`];
    const decodedToken = await jsonwebtoken.verify(refreshToken, process.env.APP_SECRET);

    const { tokenId, tokenType, userId, provider } = decodedToken;

    const user = await User.findOne({ _id: userId });

    if (!user) {
      throw new Unauthorized();
    }

    if (tokenType === 'user_refresh') {
      const newUserToken = await createUserJWT(user, provider);
      res.status(200).set('Content-Type', 'text/plain').send(newUserToken);
    } else if (tokenType === 'organisation_refresh') {
github LearningLocker / learninglocker / api / src / controllers / UploadController.js View on Github external
import * as fileService from 'lib/services/files';
import getAuthFromRequest from 'lib/helpers/getAuthFromRequest';
import catchErrors from 'api/controllers/utils/catchErrors';
import getFileFromRequest from 'api/controllers/utils/getFileFromRequest';

const uploadPeople = catchErrors(async (req, res) => {
  const authInfo = getAuthFromRequest(req);
  const file = await getFileFromRequest(req);
  const result = await fileService.uploadPeople({ authInfo, file });
  return res.status(200).json(result);
});

const uploadLogo = catchErrors(async (req, res) => {
  const orgId = req.query.data;
  const authInfo = getAuthFromRequest(req);
  const file = await getFileFromRequest(req);
  const path = await fileService.uploadLogo({ authInfo, orgId, file });
  return res.status(200).send(path);
});

export default {
  uploadPeople,
  uploadLogo
};