makeomatic/ms-files

View on GitHub
src/utils/is-alias-taken.js

Summary

Maintainability
A
0 mins
Test Coverage
const Promise = require('bluebird');
const identity = require('lodash/identity');
const { HttpStatusError } = require('common-errors');
const { FILES_USR_ALIAS_PTR, FILES_OWNER_FIELD } = require('../constant');

module.exports = function isAliasTaken(alias, customError) {
  // pass-through
  if (!alias) {
    return identity;
  }

  return function hasAlias(data) {
    const owner = data[FILES_OWNER_FIELD];
    const hash = `${FILES_USR_ALIAS_PTR}:${owner}`;
    return this.redis
      .hget(hash, alias)
      .then((uploadId) => {
        if (uploadId) {
          // could be predefined
          if (customError) throw customError;

          // throw conflict error
          const err = new HttpStatusError(409, `Alias already taken by ${uploadId} on user ${owner}`);
          err.data = { uploadId, owner };
          return Promise.reject(err);
        }

        return data;
      });
  };
};