Advertisement
Maikl5700

Node.js Sequelize

Nov 26th, 2020
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const serviceTass = require('../../services/serviceTass');
  2. const logger = require('logger').child({module: 'api', service: 'admin'});
  3. const {Sequelize} = require('../../models');
  4. const {models} = require('../../models');
  5. const {getRequestAssociationKey} = require('../../helpers/import');
  6.  
  7. const {Op} = Sequelize;
  8.  
  9. const getFilters = where => {
  10.   const filters = {};
  11.   filters[Op.and] = [];
  12.  
  13.   if (where.name) {
  14.     filters[Op.and].push({
  15.       [Op.or]: [
  16.         {
  17.           ['fullName']: {
  18.             [Op.iLike]: `%${where.name}%`,
  19.           },
  20.         },
  21.         {
  22.           ['shortName']: {
  23.             [Op.iLike]: `%${where.name}%`,
  24.           },
  25.         },
  26.       ],
  27.     });
  28.   }
  29.   if (where.InnOgrnKpp) {
  30.     filters[Op.and].push({
  31.       [Op.or]: [
  32.         {
  33.           ['inn']: {
  34.             [Op.eq]: where.InnOgrnKpp,
  35.           },
  36.         },
  37.         {
  38.           ['ogrn']: {
  39.             [Op.eq]: where.InnOgrnKpp,
  40.           },
  41.         },
  42.         {
  43.           ['kpp']: {
  44.             [Op.eq]: where.InnOgrnKpp,
  45.           },
  46.         },
  47.       ],
  48.     });
  49.   }
  50.   if (where.category) {
  51.     filters[Op.and].push({
  52.       ['category']: {
  53.         [Op.eq]: Number(where.category),
  54.       },
  55.     });
  56.   }
  57.   if (where.okved2Main) {
  58.     filters[Op.and].push({
  59.       ['okved2Main']: {
  60.         [Op.or]: where.okved2Main,
  61.       },
  62.     });
  63.   }
  64.   if (where.okved2Second) {
  65.     filters[Op.and].push({
  66.       ['okved2Second']: {
  67.         [Op.contains]: where.okved2Second,
  68.       },
  69.     });
  70.   }
  71.   if (where.region) {
  72.     filters[Op.and].push({
  73.       ['region']: {
  74.         [Op.eq]: Number(where.region),
  75.       },
  76.     });
  77.   }
  78.   if (where.address) {
  79.     filters[Op.and].push({
  80.       ['address']: {
  81.         [Op.iLike]: `%${where.address}%`,
  82.       },
  83.     });
  84.   }
  85.   if (where.industry) {
  86.     filters[Op.and].push({
  87.       ['industry']: {
  88.         [Op.eq]: where.industry,
  89.       },
  90.     });
  91.   }
  92.   if (where.companyInGISP !== undefined) {
  93.     filters[Op.and].push({
  94.       ['companyInGISP']: {
  95.         [Op.eq]: where.companyInGISP,
  96.       },
  97.     });
  98.   }
  99.   if (where.reviewed !== undefined) {
  100.     filters[Op.and].push({
  101.       ['reviewed']: {
  102.         [Op.eq]: where.reviewed,
  103.       },
  104.     });
  105.   }
  106.  
  107.   return filters;
  108. };
  109.  
  110. const methods = {
  111.   async list({where = {}, limit = 20, offset = 0, order = [['id', 'DESC']]} = {}) {
  112.     const Request = models.get('Request');
  113.     const $where = getFilters(where);
  114.  
  115.     return await Request.findAndCountAll({
  116.       limit,
  117.       offset,
  118.       order,
  119.       where: $where,
  120.     });
  121.   },
  122.   async listImportValues({where = {}, limit = 20, offset = 0, order = [['id', 'DESC']]} = {}, {token}) {
  123.     const Request = models.get('Request');
  124.     const ImportValue = models.get('ImportValue');
  125.     const ImportList = models.get('ImportList');
  126.     const $where = getFilters(where);
  127.  
  128.     const list = await ImportList.findOne({where: {id: where.listId, user: token.sub}});
  129.     if (!list) throw new Error('Перечень не найден');
  130.  
  131.     const associationKey = getRequestAssociationKey(list.dataValues.type);
  132.  
  133.     const count = await ImportValue.count({where: {listId: list.dataValues.id}});
  134.     const values = await ImportValue.findAll({
  135.       limit,
  136.       offset,
  137.       order,
  138.       where: {listId: list.dataValues.id},
  139.       raw: true,
  140.       nest: true,
  141.       include: [
  142.         {
  143.           model: Request,
  144.           required: false,
  145.           as: associationKey,
  146.           where: $where,
  147.         },
  148.       ],
  149.     });
  150.  
  151.     const rows = values
  152.       .map(row => {
  153.         if (row[associationKey].ogrn === null) {
  154.           row[associationKey].ogrn = row.value;
  155.           row[associationKey]._notFoundByImportValue = true;
  156.         }
  157.         return row[associationKey];
  158.       })
  159.       .filter(x => x);
  160.  
  161.     return {
  162.       count,
  163.       rows,
  164.     };
  165.   },
  166.   async save({id, data}) {
  167.     const Request = models.get('Request');
  168.     if (id) {
  169.       const rec = await Request.findByPk(id);
  170.       rec.set(data);
  171.       return [await rec.save(), false];
  172.     }
  173.     return [await Request.create(data), true];
  174.   },
  175.   async updateMultiple({ids = [], data}) {
  176.     const Request = models.get('Request');
  177.     const {industry, category, companyInRegion} = data;
  178.  
  179.     return Request.update(
  180.       {
  181.         industry,
  182.         category,
  183.         companyInRegion,
  184.       },
  185.       {
  186.         where: {id: ids},
  187.       },
  188.     );
  189.   },
  190.   async publishMultiple({ids}) {
  191.     if (!Array.isArray(ids)) ids = [ids];
  192.  
  193.     const Request = models.get('Request');
  194.     const Company = models.get('Company');
  195.  
  196.     const publishedRequests = [];
  197.  
  198.     for (let i = 0; i < ids.length; i++) {
  199.       const request = await Request.findByPk(ids[i]);
  200.       const foundCompany = await Company.findOne({where: {ogrn: request.ogrn}});
  201.  
  202.       serviceTass.register(request.ogrn).catch(logger.error);
  203.  
  204.       if (foundCompany) {
  205.         await foundCompany.update(request.dataValues);
  206.       } else {
  207.         await Company.create(request.dataValues);
  208.       }
  209.  
  210.       request.reviewed = Request.REVIEWED_TRUE;
  211.       request.companyInGISP = Request.COMPANY_IN_GISP_TRUE;
  212.  
  213.       publishedRequests.push(await request.save());
  214.     }
  215.  
  216.     return publishedRequests;
  217.   },
  218.   async publish({id, data}) {
  219.     const Request = models.get('Request');
  220.     const request = await Request.findByPk(id);
  221.  
  222.     serviceTass.register(data.ogrn).catch(logger.error);
  223.  
  224.     const Company = models.get('Company');
  225.     const foundCompany = await Company.findOne({where: {ogrn: data.ogrn}});
  226.  
  227.     Object.assign(data, {status: Company.STATUS_PUBLISHED});
  228.  
  229.     if (foundCompany) {
  230.       await foundCompany.update(data);
  231.     } else {
  232.       await Company.create(data);
  233.     }
  234.  
  235.     request.set(data);
  236.  
  237.     if (request && request.reviewed === Request.REVIEWED_FALSE) {
  238.       request.reviewed = Request.REVIEWED_TRUE;
  239.     }
  240.  
  241.     return request.save();
  242.   },
  243. };
  244.  
  245. module.exports = methods;
  246.  
  247. // const restructure = async () => {
  248. //   const Request = models.get('Request');
  249. //   const requests = await Request.findAll();
  250. //   requests.forEach(request => {
  251. //     Request.update({...request.data}, {where: {id: request.id}})
  252. //   });
  253. // };
  254. //
  255. // restructure().catch(console.error);
  256.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement