/var/www/qr4y.com/server/delivery/services
NameSizeModeActions
firebase/-0755rm
auth.service.js25200644editdlrm
branch.service.js99910644editdlrm
delivery.service.js125500644editdlrm
index.js9140644editdlrm
mail.service.js15320644editdlrm
order.service.js84430644editdlrm
webSocket.service.js103770644editdlrm
world.service.js22810644editdlrm
Edit: /var/www/qr4y.com/server/delivery/services/branch.service.js (9991B)
const logger = require('../logger')({ service: 'BranchService' }) const { Op } = require('sequelize') const PointInPolygon = require('point-in-polygon') const { isSet, isArray } = require('../api/utils/validator.util') module.exports = class BranchService { constructor({ db, config }) { this.db = db this.config = config this.branches = VARS.branches logger.child({ service: 'BranchService', method: 'Constructor' }).info('Success') } async Run() { // загрузить отделения this.branches = await this.db.branch.findAll({ include: [ { model: this.db.company, as: 'company', where: { deletedAt: null }, include: [ this.db.companyType, this.db.companyImage, this.db.language ] }, { model: this.db.branchDeliveryTypes, include: [ this.db.deliveryType, { model: this.db.branchPayTypes, include: [ this.db.payType ] } ] }, this.db.address, this.db.room ] }) for (let branch of this.branches) { branch.company.users = (await this.db.companyUsers.findAll({ where: { companyId: branch.company.id }, limit: 10 })) } logger .child({ service: 'BranchService', method: 'Run' }) .info(`Branches loaded: ${this.branches?.length || 0}.`) } async Stop() { this.interval = null this.branches = [] logger.child({ service: 'BranchService', method: 'Stop' }).info('Success') } async LoadBranch(id) { var b = await this.db.branch.findOne({ where: { id }, include: [ { model: this.db.company, as: 'company', where: { deletedAt: null }, include: [ this.db.companyType, this.db.companyImage, this.db.language ] }, { model: this.db.branchDeliveryTypes, include: [ this.db.deliveryType, { model: this.db.branchPayTypes, include: [ this.db.payType ] } ] }, this.db.address, this.db.room ] }) b.company.users = (await this.db.companyUsers.findAll({ where: { companyId: b.company.id }, limit: 10 })) return b } async GetBranch(id) { let branch = this.branches.find((x) => x.id === id) if (!branch) { branch = await this.LoadBranch(id) if (!branch) { logger .child({ service: 'BranchService', method: 'GetBranch', id: id }) .info('Not found') return } await this.AddBranch(branch) logger .child({ service: 'BranchService', method: 'GetBranch', id: branch.id }) .info('Success') } return branch } async GetBranchById(id) { let branch = this.branches.find((x) => x.id === id) if (!branch) { branch = await this.LoadBranch(id) if (!branch) { logger .child({ service: 'BranchService', method: 'GetBranch', id: branch.id }) .info('Not found') return } await this.AddBranch(branch) logger .child({ service: 'BranchService', method: 'GetBranch', id: branch.id }) .info('Success') } return branch } async AddBranch(branch) { if (branch) { const idx = this.branches.findIndex((p) => p.id == branch.id) if (idx === -1) { this.branches.push(branch) logger .child({ service: 'BranchService', method: 'AddBranch', id: branch.id }) .info('Success') }else{ this.branches[idx] = branch } } } async ReloadBranch(id) { let branch = await this.LoadBranch(id) await this.AddBranch(branch) return branch } async RemoveBranch(id) { const idx = this.branches.findIndex((p) => p.id == id) if (idx !== -1) { this.branches.splice(idx, 1) logger .child({ service: 'BranchService', method: 'RemoveBranch', id: id }) .info('Success') } } async GetBranches() { logger.child({ service: 'BranchService', method: 'GetBranches' }).info('Success') return this.branches } async GetBranchesByArea({ user, active, working, latitude, longtitude, countryCodeId, companyTypeId, deliveryTypeId, page, limit, sort }) { let listAll = [] let listAllNoDistance = [] for (let branch of this.branches) { // Company check if (branch.company == null) continue; // Country check let foundByCountry = true; if (countryCodeId != null && countryCodeId.length > 0) { foundByCountry = countryCodeId == branch.company.countryCodeId; } if (!foundByCountry) continue; // Company type check let foundByCompanyType = true; if (companyTypeId != null && companyTypeId.length > 0) { foundByCompanyType = false; } if (branch.company.companyTypes != null) { for (let ct of branch.company.companyTypes) { if (ct.id == companyTypeId) { foundByCompanyType = true; } } } // Delivery type check let foundByDeliveryType = true; if (deliveryTypeId != null && deliveryTypeId.length > 0) { foundByDeliveryType = false; } if (!foundByDeliveryType && branch.branchDeliveryTypes != null) { for (let ct of branch.branchDeliveryTypes) { if (ct.deliveryTypeId == deliveryTypeId) { foundByDeliveryType = true; } } } // Own company ADD branch.ownCompany = false; if (foundByDeliveryType && foundByCompanyType && user != null) { // IF SUPERUSER // if (user.roleId == '1') { // listAll.push(branch) // } else { // IF USER COMPANY let add = false; for (let cu of branch.company.users) { if (cu.userId == user.id) { branch.ownCompany = true; listAll.push(branch); add = true; continue; } } // } if (add) continue; } if (!foundByCompanyType||!foundByDeliveryType) continue; if (isSet(active)) { const act = (active === 'true' || active == 1) if (branch.active != act) continue; if (branch.company.active != act) continue; } if (isSet(working)) { const work = (working === 'true' || working == 1) if (branch.company.enabled != work) continue; } // ALL listAllNoDistance.push(branch) // BY RADIUS if (branch.searchType == 1) { if (branch.address == null) continue; const dist = GetDistance( branch.address.latitude, branch.address.longtitude, latitude, longtitude ) try { if (dist < parseFloat(branch.radius)) { listAll.push(branch) } }catch(e){} } // BY AREA if (branch.searchType == 2) { if (!foundByCompanyType || branch.area == null || branch.area == '') continue let polygon = JSON.parse(branch.area) let area = [] if (isArray(polygon) && polygon[0].length > 0) { for (let p of polygon) { if (isSet(p) && typeof p === 'string') { const point = p.split(',') area.push([parseFloat(point[0]), parseFloat(point[1])]) } } if (latitude != '' && latitude != '0.0') { let res = PointInPolygon([latitude, longtitude], area) if (res) { listAll.push(branch) } } else { // Если нет координат отдаем по лимиту listAll.push(branch) } } } } if (listAll.length > 0) { // console.log('-----'); // for (var b in listAll) { // console.log(listAll[b].name); // } // if (isSet(active)) { // listAll = listAll.filter((p) => p.active == (active === 'true' || active == 1)) // } // if (isSet(working)) { // listAll = listAll.filter( // (p) => p.dataValues.working == (working === 'true' || working == 1) // ) // } // listAll.sort((a, b) => { // return ( // (a.id == b.id ? 0 : a.id > b.id ? 1 : -1) * // (sort.toLowerCase() === 'desc' ? -1 : 1) // ) // }) return listAll.slice(page - 1 * limit, limit * page) } else { if (listAllNoDistance.length > 0) { return listAllNoDistance.slice(page - 1 * limit, limit * page) } } return listAll } async GetBranchByDistance({ latitude, longtitude }) {} // GetBranchWorkTime(branch) { // switch (branch.work_mode) { // case '24hours': // return { working: true, text: '24/7' } // break // case 'graphic': // const now = new Date() // const day = now.getDay() // const hour = now.getHours() // const minute = now.getMinutes() // let text = '' // let working = false // for (let wt of branch.workTimes) { // if (wt.dayOfWeek == day) { // const start_time = wt.startTime.split(':') // const finish_time = wt.finishTime.split(':') // const start_time_break = wt.startTimeBreak.split(':') // const finish_time_break = wt.finishTimeBreak.split(':') // var Time = function (timeString) { // var t = timeString.split(':') // this.hour = parseInt(t[0]) // this.minutes = parseInt(t[1]) // this.isBiggerThan = function (other) { // return ( // this.hour > other.hour || // (this.hour === other.hour && this.minutes > other.minutes) // ) // } // } // var timeIsBetween = function (start, end, check) { // return start.hour <= end.hour // ? check.isBiggerThan(start) && !check.isBiggerThan(end) // : (check.isBiggerThan(start) && check.isBiggerThan(end)) || // (!check.isBiggerThan(start) && !check.isBiggerThan(end)) // } // var openTime = new Time(wt.startTime) // var closeTime = new Time(wt.finishTime) // var breakTime = new Time(wt.startTimeBreak) // var unbreakTime = new Time(wt.finishTimeBreak) // var checkTime = new Time(hour + ':' + minute) // var isBetweenOpen = timeIsBetween(openTime, closeTime, checkTime) // var isBetweenBreak = timeIsBetween(breakTime, unbreakTime, checkTime) // text = wt.startTime + '-' + wt.finishTime // "isBetween: "+isBetweenOpen+" isBreak: "+isBetweenBreak // if (isBetweenOpen && !isBetweenBreak) working = true // } // } // return { working: working, text: text } // default: // return { working: false, text: '' } // } // } }