insatller fee

This commit is contained in:
morteza-mortezai
2025-11-26 09:37:16 +03:30
parent 1a98067f1a
commit 7b501107cf
11 changed files with 224 additions and 63 deletions
+9 -2
View File
@@ -337,13 +337,13 @@
:badge="pendingUsers ? { text: pendingUsers, color: 'danger' } : null"
/>
<CSidebarNavItem
<!-- <CSidebarNavItem
v-if="hasPermission('gps')"
name="دستگاه ها"
:to="{ name: 'admin-gps-device' }"
font-icon="fal fa-microchip"
:exact="false"
/>
/> -->
<CSidebarNavItem
v-if="hasPermission('gps')"
name="دستگاه های ثبت شده"
@@ -352,6 +352,13 @@
:badge="newInstallDeviceRequest ? { text: newInstallDeviceRequest, color: 'danger' } : null"
:exact="false"
/>
<CSidebarNavItem
v-if="hasPermission('gps')"
name="هزینه نصب"
:to="{ name: 'admin-gps-fee' }"
font-icon="fal fa-money-bill"
:exact="false"
/>
<CSidebarNavItem
v-if="hasPermission('gps')"
name="درخواست های برداشت"
+106
View File
@@ -0,0 +1,106 @@
<template>
<div>
<CustomSubHeader>
<CBreadcrumb class="border-0 mb-0"> تنظیم هزینه نصب </CBreadcrumb>
</CustomSubHeader>
<CRow>
<CCol xl="6">
<CCard>
<CCardHeader>
<slot name="header">
<i class="fas fa-filter"></i>
<b>فیلتر ها</b>
</slot>
</CCardHeader>
<CCardBody>
<CRow>
<CCol sm="12">
</CCol>
<CCol sm="12">
<el-divider></el-divider>
</CCol>
<CCol sm="12">
<el-form :model="form" :rules="rules" ref="form">
<el-form-item label="مبلغ نصب" prop="amount">
<el-input v-model="form.amount" placeholder="مبلغ"></el-input>
<span v-if="form.amount">{{ formatCurrency(form.amount) }}</span>
</el-form-item>
<el-form-item >
<el-button type="primary" @click="setFee">ثبت</el-button>
</el-form-item>
</el-form>
</CCol>
</CRow>
</CCardBody>
</CCard>
</CCol>
</CRow>
</div>
</template>
<script>
export default {
name: 'AdminGpsWithdrawList',
layout: 'admin',
data() {
return {
form: {
amount: 0
},
rules: {
amount: [{ required: true, message: 'لطفا مبلغ را وارد کنید', trigger: 'blur' }]
}
}
},
methods: {
async setFee() {
try {
await this.$axios.post('/api/admin/gps/install-fee', this.form)
.then(res => {
this.$message({
type: 'success',
message: 'هزینه نصب با موفقیت ثبت شد'
})
this.$nuxt.refresh()
})
} catch (error) {
console.log(error)
}
},
async getFee() {
try {
await this.$axios.get('/api/admin/gps/install-fee')
.then(res => {
this.form.amount = res.data.amount
})
} catch (error) {
console.log(error)
}
},
formatCurrency(amount) {
const formatted= new Intl.NumberFormat('fa-IR').format(amount);
return `${formatted} تومان`;
}
},
mounted() {
this.getFee()
}
}
</script>
<style>
.unreadMsgInCv {
background: rgba(orange, 0.1) !important;
}
</style>
+3 -2
View File
@@ -65,6 +65,7 @@
<el-table-column label="مدل" width="150px">
<template slot-scope="scope">
{{ scope?.row?.deviceId?.model }}
{{ scope?.row?.deviceType }}
</template>
</el-table-column>
<el-table-column prop="IMEI" label=" IMEI" width="150px"> </el-table-column>
@@ -106,7 +107,7 @@
</el-table-column>
<el-table-column label="تایید" width="110" align="center">
<template slot-scope="scope">
<CButton
<!-- <CButton
v-if="scope?.row?.status == 0 || scope?.row?.status == 2"
:key="scope?.row?._id"
color="success"
@@ -114,7 +115,7 @@
@click="post(scope?.row?._id, 'accept')"
>
<i class="fa fa-check-square"></i>
</CButton>
</CButton> -->
<CButton
v-if="scope?.row?.status == 0 || scope?.row?.status == 1"
:key="scope?.row?._id"
+5
View File
@@ -143,6 +143,11 @@
<el-tag v-if="scope.row.status == 0" type="warning"> درانتظار</el-tag>
</template>
</el-table-column>
<el-table-column label="تاریخ درخواست" width="">
<template slot-scope="scope">
{{new Date(scope.row.created_at).toLocaleString('fa-IR') }}
</template>
</el-table-column>
<el-table-column label="بررسی" width="110" align="center">
<template slot-scope="scope">
<CButton :key="scope.row._id" color="info" variant="outline" @click="showDialog(scope.row)">
+25
View File
@@ -0,0 +1,25 @@
const { body, validationResult } = require('express-validator')
const Fee = require('../models/GPS.Fee')
const titleKey = 'install_fee'
const { checkValidations } = require('../plugins/controllersHelperFunctions')
module.exports.setFee = [
[body('amount').notEmpty().isInt().withMessage('مقدار هزینه را وارد کنید')],
checkValidations(validationResult),
async (req, res) => {
const { amount } = req.body
const fee = await Fee.findOne({ title: titleKey })
if (fee) {
fee.amount = amount
await fee.save()
return res.status(200).json(fee)
}
const newFee = await Fee.create({ title: titleKey, amount })
res.status(201).json(newFee)
}
]
module.exports.getFee = async (req, res) => {
const fee = await Fee.findOne({ title: titleKey })
res.status(200).json(fee)
}
+45 -46
View File
@@ -4,64 +4,63 @@ const User = require('../models/GPS.User')
const InstalledDevice = require('../models/GPS.InstalledDevice')
const Device = require('../models/GPS.Device')
const userGPS = require('../models/GPS.User')
const Fee = require('../models/GPS.Fee')
const Score = require('../models/GPS.Score')
const { _sr } = require('../plugins/serverResponses')
const { notifyAdmin } = require('../WebSocket/controllers/admin_EventsController')
const { isDeviceRegisteredOnKoja } = require('../kojaWebService')
const { findDeviceByImeiOnKoja } = require('../kojaWebService')
module.exports.deviceRegistration = [
[body('IMEI').notEmpty().isString().withMessage('لطفا IMEI را وارد یا تصحیح کنید')],
checkValidations(validationResult),
async (req, res) => {
const IMEI = req.body.IMEI
console.log('IMEI ******************', IMEI)
try {
const device = await Device.findOne({ IMEI })
if (!device) {
return res.status(404).json({ msg: 'این IMEI وجود ندارد' })
}
if (device.status === 1) {
const installedDevice = await InstalledDevice.findOne({ IMEI })
// // const installedDevice = false
if (installedDevice) {
return res.status(400).json({ msg: 'این IMEI قبلا ثبت شده ' })
}
const isRegistered = await isDeviceRegisteredOnKoja(IMEI)
if (isRegistered) {
let installedDeviceId = null
try {
const data = {
deviceId: device.id,
status: 0,
registerDate: new Date(),
userId: req.user_id,
IMEI: req.body.IMEI
}
// 1.add to installed device
const installedDevice = await InstalledDevice.create(data)
installedDeviceId = installedDevice._id
// 2. change status
await Device.findOneAndUpdate({ IMEI }, { status: 1 })
// 3. add to wallet
await userGPS.findByIdAndUpdate(req.user_id, { $inc: { walletBalance: 100_000 } })
notifyAdmin('newInstallDeviceRequest')
res.status(201).json({ msg: 'مبلغ صد هزار تومان به کیف پول شما اضافه شد' })
} catch (err) {
console.log('Error in Db Transactions', err?.message)
if (installedDeviceId) {
try {
await InstalledDevice.deleteOne({ _id: installedDeviceId })
console.log('Rollback 1/2: InstalledDevice deleted.')
await Device.findOneAndUpdate({ IMEI }, { status: 0 })
console.log('Rollback 2/2: Device status reset to 0.')
} catch (error) {
console.error('CRITICAL: Rollback failed for some changes. MANUAL INTERVENTION REQUIRED.', error)
}
}
return res.status(err?.status || 500).json({ msg: err?.msg || 'خطا در تراکنش های دیتابیس ' })
// const isRegisteredOnKoja = true
const RegisteredDevice = await findDeviceByImeiOnKoja(IMEI)
if (!RegisteredDevice) {
return res.status(400).json({ msg: 'هنوز در سامانه ردیاب ثبت نشده است.' })
}
let installedDeviceId = null
try {
const fee = await Fee.findOne({ title: 'install_fee' })
if (!fee) {
return res.status(400).json({ msg: 'هزینه نصب یافت نشد' })
}
} else {
res.status(400).json({ msg: 'هنوز در سامانه ردیاب ثبت نشده است.' })
const data = {
// deviceId: RegisteredDevice.deviceId,
deviceType: RegisteredDevice.device_type.name,
status: 1,
registerDate: new Date(),
userId: req.user_id,
IMEI: req.body.IMEI
}
// 1.add to installed device
const installedDevice = await InstalledDevice.create(data)
installedDeviceId = installedDevice._id
// 2. add to wallet
await userGPS.findByIdAndUpdate(req.user_id, { $inc: { walletBalance:fee.amount } })
notifyAdmin('newInstallDeviceRequest')
res.status(201).json({ msg: 'مبلغ صد هزار تومان به کیف پول شما اضافه شد' })
} catch (err) {
console.log('Error in Db Transactions', err?.message)
if (installedDeviceId) {
try {
await InstalledDevice.deleteOne({ _id: installedDeviceId })
console.log('Rollback: InstalledDevice deleted.')
} catch (error) {
console.error('CRITICAL: Rollback failed for some changes. MANUAL INTERVENTION REQUIRED.', error)
}
}
return res.status(err?.status || 500).json({ msg: err?.msg || 'خطا در تراکنش های دیتابیس ' })
}
} catch (err) {
console.log('deviceRegistration Error: ', err?.message)
@@ -147,7 +146,7 @@ module.exports.getAllForAdmin = async (req, res) => {
// }
const data = await InstalledDevice.find()
// .sort({ [sortField]: sortOrder })
.sort({ created_at: -1 })
.populate('deviceId')
.populate('userId', 'shopName national_code first_name last_name mobile_number city_name province_name')
// .skip(skip)
+6
View File
@@ -18,6 +18,12 @@ module.exports.createRequest = [
res.status(400).json({ msg: 'شما درخواست درحال بررسی دارید' })
} else {
const user = await User.findById(req.user_id)
if (!user) {
return res.status(400).json({ msg: 'کاربر یافت نشد' })
}
if (req.body.amount > user.walletBalance) {
return res.status(400).json({ msg: 'موجودی شما کافی نیست' })
}
if (user.walletBalance >= 50000) {
const data = {
card: req.body.card,
+10 -12
View File
@@ -1,29 +1,27 @@
const axios = require('axios')
const { kojaBaseUrl, kojaAccount } = require('./_env')
export async function isDeviceRegisteredOnKoja(imei) {
export async function findDeviceByImeiOnKoja(imei) {
try {
const result = await axios.get(`/devices/imei/${imei}`, {
baseURL: kojaBaseUrl,
auth: kojaAccount
})
console.log('device registration check result from koja.ir', result)
if (result.status === 200) {
return true
}
return result
console.log('result.data from koja.ir******************', result.data.result)
return result.data.result
} catch (error) {
if (error.response) {
const { status } = error.response
if (status === 401) {
throw new Error('خطا در اتصال به سامانه ردیاب')
}
if (status === 404) {
return false
}
if (status === 403) {
return true
}
// if (status === 404) {
// return false
// }
// if (status === 403) {
// return true
// }
throw error
} else {
throw error
+10
View File
@@ -0,0 +1,10 @@
const mongoose = require('mongoose')
const FeeSchema = mongoose.Schema({
title: String,
amount: Number,
createdAt: { type: Date, default: Date.now },
})
module.exports = mongoose.model('Fee', FeeSchema)
+1
View File
@@ -6,6 +6,7 @@ const InstalledDeviceSchema = mongoose.Schema({
ref: "Device"
},
IMEI:String,
deviceType:String,
status: {
type: Number,
enum: [
+4 -1
View File
@@ -33,7 +33,7 @@ const award = require('../controllers/GPS.Award')
const awardRequest = require('../controllers/GPS.AwardRequest')
const userGPS = require('../controllers/GPS.User')
const renewal = require('../controllers/GPS.Renewal')
const fee = require('../controllers/GPS.Fee')
/// //////////////////////////////////////////////////////// routes
/// ///////////////////////////// GPS
@@ -81,6 +81,9 @@ router.get('/gps/device/imei/:imei', hasPermission('gps'), device.getOneByIMEI)
router.get('/gps/device/id/:id', hasPermission('gps'), device.getOne)
router.put('/gps/device/:id', hasPermission('gps'), device.updateDevice)
/// ////////////// Fee
router.post('/gps/install-fee', hasPermission('gps'), fee.setFee)
router.get('/gps/install-fee', hasPermission('gps'), fee.getFee)
/// /////////////////////////////////////////////////////////
/// ///////////// survey
router.get('/surveys', hasPermission('surveys'), surveyController.getAllForAdmin)