56 lines
1.2 KiB
JavaScript
56 lines
1.2 KiB
JavaScript
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));
|