new cron scripts
This commit is contained in:
55
cron/configUpdate.js
Normal file
55
cron/configUpdate.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
const permitsModel = mongoose.model(
|
||||
'permit',
|
||||
new mongoose.Schema({}, { strict: false })
|
||||
);
|
||||
|
||||
const configModel = mongoose.model(
|
||||
'config',
|
||||
new mongoose.Schema({
|
||||
tenantId: {
|
||||
type: String,
|
||||
unique: true,
|
||||
},
|
||||
emailIds: Array,
|
||||
statusMap: Object,
|
||||
folders: Object,
|
||||
updatedAt: Date,
|
||||
updatedBy: {
|
||||
type: mongoose.Types.ObjectId,
|
||||
ref: 'user',
|
||||
},
|
||||
}),
|
||||
'config'
|
||||
);
|
||||
|
||||
(async () => {
|
||||
console.log(new Date().toISOString() + ' Started status script...');
|
||||
|
||||
await mongoose.connect(process.env.DB_URI);
|
||||
|
||||
const uniqueStatus = await permitsModel.distinct('status');
|
||||
const config = await configModel.findOne({ tenantId: 'arf4w59nzduytv7' });
|
||||
|
||||
const statusMap = config.statusMap;
|
||||
|
||||
let allStatus = [];
|
||||
for (const status in statusMap) {
|
||||
allStatus.push(...statusMap[status]);
|
||||
}
|
||||
|
||||
const notSet = [];
|
||||
if (statusMap.NotSet) notSet.push(...statusMap.NotSet);
|
||||
|
||||
for (const status of uniqueStatus) {
|
||||
if (!allStatus.includes(status)) notSet.push(status);
|
||||
}
|
||||
|
||||
await configModel.updateOne(
|
||||
{ tenantId: 'arf4w59nzduytv7' },
|
||||
{ $set: { 'statusMap.NotSet': notSet } }
|
||||
);
|
||||
|
||||
await mongoose.connection.close();
|
||||
})().catch((err) => console.log(err));
|
||||
68
cron/statusUpdate.js
Normal file
68
cron/statusUpdate.js
Normal file
@@ -0,0 +1,68 @@
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
const permitsModel = mongoose.model(
|
||||
'permit',
|
||||
new mongoose.Schema(
|
||||
{
|
||||
status: String,
|
||||
cleanStatus: String,
|
||||
},
|
||||
{ strict: false }
|
||||
)
|
||||
);
|
||||
|
||||
const configModel = mongoose.model(
|
||||
'config',
|
||||
new mongoose.Schema({
|
||||
tenantId: {
|
||||
type: String,
|
||||
unique: true,
|
||||
},
|
||||
emailIds: Array,
|
||||
statusMap: Object,
|
||||
folders: Object,
|
||||
updatedAt: Date,
|
||||
updatedBy: {
|
||||
type: mongoose.Types.ObjectId,
|
||||
ref: 'user',
|
||||
},
|
||||
}),
|
||||
'config'
|
||||
);
|
||||
|
||||
(async () => {
|
||||
console.log(new Date().toISOString() + ' Started status script...');
|
||||
|
||||
await mongoose.connect(process.env.DB_URI);
|
||||
|
||||
const config = await configModel.findOne({ tenantId: 'arf4w59nzduytv7' });
|
||||
const permits = await permitsModel.find();
|
||||
|
||||
const statusMap = config.statusMap;
|
||||
const flatMap = {};
|
||||
|
||||
for (const status in statusMap) {
|
||||
for (const countyStatus of statusMap[status]) {
|
||||
flatMap[countyStatus] = status;
|
||||
}
|
||||
}
|
||||
|
||||
for (const permit of permits) {
|
||||
try {
|
||||
const cleanStatus = flatMap[permit.status];
|
||||
if (!cleanStatus)
|
||||
throw new Error(
|
||||
`Invalid status: ${permit.status} | recId: ${permit.pid}`
|
||||
);
|
||||
|
||||
await permitsModel.updateOne(
|
||||
{ pid: permit.pid },
|
||||
{ $set: { cleanStatus: cleanStatus } }
|
||||
);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
|
||||
await mongoose.connection.close();
|
||||
})().catch((err) => console.log(err));
|
||||
Reference in New Issue
Block a user