All files / backend/controllers manifestMutator.mjs

100% Statements 61/61
96.42% Branches 27/28
100% Functions 10/10
100% Lines 60/60

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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187                                    1x                                                       15x 15x     15x   15x 32x 32x   32x 26x 26x     6x   9x       3x 15x 15x         15x 15x   15x 4x   11x       6x 6x   6x 2x   4x         7x 7x   7x 7x 7x   7x   7x 2x     5x 1x     4x 4x       4x 4x   4x 4x   4x   4x         3x   1x   1x       4x 4x   4x 4x   4x   4x         3x     1x 1x       15x       1x   1x 1x                                   1x    
/*
  Copyright (C) 2023 Sber
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
 
import { Router } from 'express';
 
const RESPONSE_STATUSES = {
  200: {
    code: 200,
    message: 'Атрибут объекта успешно обновлен'
  },
  201: {
    code: 201,
    message: 'Атрибут объекта успешно создан'
  },
  204: {
    code: 204,
    message: 'Атрибут объекта успешно удален'
  },
  400: {
    code: 400,
    message: 'Некорректный запрос'
  },
  404: {
    code: 404,
    message: 'Ресурс не найден'
  },
  409: {
    code: 409,
    message: 'Аттрибут с таким именем уже существует'
  }
};
 
function getObjectEntityByChainPath(object = {}, chainPath) {
  const SEPARATION_SYMBOL = '/';
  let currentEntity = object;
  let childEntity;
 
  const list = chainPath.split(SEPARATION_SYMBOL);
 
  for (let i = 0; i < list.length; i++) {
    const path = list[i];
    childEntity = currentEntity[path];
 
    if (childEntity && typeof childEntity === 'object') {
      currentEntity = childEntity;
      continue;
    }
 
    return;
  }
  return currentEntity;
}
 
function addDataLakeToRequest(manifestObject) {
  return function(req, res, next) {
    req.dataLake = manifestObject;
    next();
  };
}
 
function validateBody(req, res, next) {
  const errorStatus = RESPONSE_STATUSES['400'];
  const { object, attribute, value } = req.body;
 
  if (!object || !attribute || value === undefined) {
    return handleError(res, errorStatus);
  }
  next();
}
 
function validateParams(req, res, next) {
  const errorStatus = RESPONSE_STATUSES['404'];
  const { object, attributeKey } = req.query;
 
  if (!object || !attributeKey) {
    return handleError(res, errorStatus);
  }
  next();
}
 
// controllers
function createEntityAttributeInDataLake(req, res) {
  const { dataLake, body } = req;
  const { object, attribute, value } = body;
 
  const alreadyExistError = RESPONSE_STATUSES['409'];
  const notFountError = RESPONSE_STATUSES['404'];
  const successStatus = RESPONSE_STATUSES['201'];
 
  const entityForMutation = getObjectEntityByChainPath(dataLake, object);
 
  if (!entityForMutation) {
    return handleError(res, notFountError);
  }
 
  if (entityForMutation[attribute]) {
    return handleError(res, alreadyExistError);
  }
 
  entityForMutation[attribute] = value;
  res.status(successStatus.code).json(successStatus);
}
 
function deleteEntityAttributeInDataLake(req, res) {
  const { dataLake, query } = req;
  const { object, attributeKey } = query;
 
  const notFountError = RESPONSE_STATUSES['404'];
  const successStatus = RESPONSE_STATUSES['204'];
 
  const entityForMutation = getObjectEntityByChainPath(dataLake, object);
 
  if (
    !entityForMutation ||
    (typeof entityForMutation === 'object' &&
      !Object.keys(entityForMutation).includes(attributeKey))
  ) {
    return handleError(res, notFountError);
  }
  delete entityForMutation[attributeKey];
 
  res.status(successStatus.code).json(successStatus);
}
 
function updateEntityAttributeInDataLake(req, res) {
  const { dataLake, body } = req;
  const { object, attribute, value } = body;
 
  const successStatus = RESPONSE_STATUSES['200'];
  const errorStatus = RESPONSE_STATUSES['404'];
 
  const entityForMutation = getObjectEntityByChainPath(dataLake, object);
 
  if (
    !entityForMutation ||
    (typeof entityForMutation === 'object' &&
      !Object.keys(entityForMutation).includes(attribute))
  ) {
    return handleError(res, errorStatus);
  }
 
  entityForMutation[attribute] = value;
  res.status(successStatus.code).json(successStatus);
}
 
function handleError(res, status) {
  res.status(status.code).json(status);
}
 
export default function(app) {
  const { manifest } = app.storage;
 
  const router = Router();
  router
    .route('/')
    .post(
      validateBody,
      addDataLakeToRequest(manifest),
      createEntityAttributeInDataLake
    )
    .delete(
      validateParams,
      addDataLakeToRequest(manifest),
      deleteEntityAttributeInDataLake
    )
    .patch(
      validateBody,
      addDataLakeToRequest(manifest),
      updateEntityAttributeInDataLake
    );
 
  app.use(['/seaf-core/api/manifest-mutation', '/manifest-mutation'], router);
}