Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const serviceTass = require('../../services/serviceTass');
- const logger = require('logger').child({module: 'api', service: 'admin'});
- const {Sequelize} = require('../../models');
- const {models} = require('../../models');
- const {getRequestAssociationKey} = require('../../helpers/import');
- const {Op} = Sequelize;
- const getFilters = where => {
- const filters = {};
- filters[Op.and] = [];
- if (where.name) {
- filters[Op.and].push({
- [Op.or]: [
- {
- ['fullName']: {
- [Op.iLike]: `%${where.name}%`,
- },
- },
- {
- ['shortName']: {
- [Op.iLike]: `%${where.name}%`,
- },
- },
- ],
- });
- }
- if (where.InnOgrnKpp) {
- filters[Op.and].push({
- [Op.or]: [
- {
- ['inn']: {
- [Op.eq]: where.InnOgrnKpp,
- },
- },
- {
- ['ogrn']: {
- [Op.eq]: where.InnOgrnKpp,
- },
- },
- {
- ['kpp']: {
- [Op.eq]: where.InnOgrnKpp,
- },
- },
- ],
- });
- }
- if (where.category) {
- filters[Op.and].push({
- ['category']: {
- [Op.eq]: Number(where.category),
- },
- });
- }
- if (where.okved2Main) {
- filters[Op.and].push({
- ['okved2Main']: {
- [Op.or]: where.okved2Main,
- },
- });
- }
- if (where.okved2Second) {
- filters[Op.and].push({
- ['okved2Second']: {
- [Op.contains]: where.okved2Second,
- },
- });
- }
- if (where.region) {
- filters[Op.and].push({
- ['region']: {
- [Op.eq]: Number(where.region),
- },
- });
- }
- if (where.address) {
- filters[Op.and].push({
- ['address']: {
- [Op.iLike]: `%${where.address}%`,
- },
- });
- }
- if (where.industry) {
- filters[Op.and].push({
- ['industry']: {
- [Op.eq]: where.industry,
- },
- });
- }
- if (where.companyInGISP !== undefined) {
- filters[Op.and].push({
- ['companyInGISP']: {
- [Op.eq]: where.companyInGISP,
- },
- });
- }
- if (where.reviewed !== undefined) {
- filters[Op.and].push({
- ['reviewed']: {
- [Op.eq]: where.reviewed,
- },
- });
- }
- return filters;
- };
- const methods = {
- async list({where = {}, limit = 20, offset = 0, order = [['id', 'DESC']]} = {}) {
- const Request = models.get('Request');
- const $where = getFilters(where);
- return await Request.findAndCountAll({
- limit,
- offset,
- order,
- where: $where,
- });
- },
- async listImportValues({where = {}, limit = 20, offset = 0, order = [['id', 'DESC']]} = {}, {token}) {
- const Request = models.get('Request');
- const ImportValue = models.get('ImportValue');
- const ImportList = models.get('ImportList');
- const $where = getFilters(where);
- const list = await ImportList.findOne({where: {id: where.listId, user: token.sub}});
- if (!list) throw new Error('Перечень не найден');
- const associationKey = getRequestAssociationKey(list.dataValues.type);
- const count = await ImportValue.count({where: {listId: list.dataValues.id}});
- const values = await ImportValue.findAll({
- limit,
- offset,
- order,
- where: {listId: list.dataValues.id},
- raw: true,
- nest: true,
- include: [
- {
- model: Request,
- required: false,
- as: associationKey,
- where: $where,
- },
- ],
- });
- const rows = values
- .map(row => {
- if (row[associationKey].ogrn === null) {
- row[associationKey].ogrn = row.value;
- row[associationKey]._notFoundByImportValue = true;
- }
- return row[associationKey];
- })
- .filter(x => x);
- return {
- count,
- rows,
- };
- },
- async save({id, data}) {
- const Request = models.get('Request');
- if (id) {
- const rec = await Request.findByPk(id);
- rec.set(data);
- return [await rec.save(), false];
- }
- return [await Request.create(data), true];
- },
- async updateMultiple({ids = [], data}) {
- const Request = models.get('Request');
- const {industry, category, companyInRegion} = data;
- return Request.update(
- {
- industry,
- category,
- companyInRegion,
- },
- {
- where: {id: ids},
- },
- );
- },
- async publishMultiple({ids}) {
- if (!Array.isArray(ids)) ids = [ids];
- const Request = models.get('Request');
- const Company = models.get('Company');
- const publishedRequests = [];
- for (let i = 0; i < ids.length; i++) {
- const request = await Request.findByPk(ids[i]);
- const foundCompany = await Company.findOne({where: {ogrn: request.ogrn}});
- serviceTass.register(request.ogrn).catch(logger.error);
- if (foundCompany) {
- await foundCompany.update(request.dataValues);
- } else {
- await Company.create(request.dataValues);
- }
- request.reviewed = Request.REVIEWED_TRUE;
- request.companyInGISP = Request.COMPANY_IN_GISP_TRUE;
- publishedRequests.push(await request.save());
- }
- return publishedRequests;
- },
- async publish({id, data}) {
- const Request = models.get('Request');
- const request = await Request.findByPk(id);
- serviceTass.register(data.ogrn).catch(logger.error);
- const Company = models.get('Company');
- const foundCompany = await Company.findOne({where: {ogrn: data.ogrn}});
- Object.assign(data, {status: Company.STATUS_PUBLISHED});
- if (foundCompany) {
- await foundCompany.update(data);
- } else {
- await Company.create(data);
- }
- request.set(data);
- if (request && request.reviewed === Request.REVIEWED_FALSE) {
- request.reviewed = Request.REVIEWED_TRUE;
- }
- return request.save();
- },
- };
- module.exports = methods;
- // const restructure = async () => {
- // const Request = models.get('Request');
- // const requests = await Request.findAll();
- // requests.forEach(request => {
- // Request.update({...request.data}, {where: {id: request.id}})
- // });
- // };
- //
- // restructure().catch(console.error);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement