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));
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from "zod";
|
||||
import mongoose from "mongoose";
|
||||
import { buildJsonSchemas } from "fastify-zod";
|
||||
import { pageMetadata, pageQueryParams } from "../pagination";
|
||||
import { z } from 'zod';
|
||||
import mongoose from 'mongoose';
|
||||
import { buildJsonSchemas } from 'fastify-zod';
|
||||
import { pageMetadata, pageQueryParams } from '../pagination';
|
||||
|
||||
const permitSchema = new mongoose.Schema({
|
||||
tenantId: {
|
||||
@@ -16,7 +16,7 @@ const permitSchema = new mongoose.Schema({
|
||||
county: Object,
|
||||
client: {
|
||||
type: mongoose.Types.ObjectId,
|
||||
ref: "organization",
|
||||
ref: 'organization',
|
||||
},
|
||||
clientData: Object,
|
||||
permitDate: Date,
|
||||
@@ -29,10 +29,11 @@ const permitSchema = new mongoose.Schema({
|
||||
),
|
||||
status: String,
|
||||
manualStatus: String,
|
||||
cleanStatus: String,
|
||||
utility: String,
|
||||
assignedTo: {
|
||||
type: mongoose.Types.ObjectId,
|
||||
ref: "user",
|
||||
ref: 'user',
|
||||
},
|
||||
link: String,
|
||||
address: Object,
|
||||
@@ -50,7 +51,7 @@ const permitSchema = new mongoose.Schema({
|
||||
createdAt: Date,
|
||||
createdBy: {
|
||||
type: mongoose.Types.ObjectId,
|
||||
ref: "user",
|
||||
ref: 'user',
|
||||
},
|
||||
newProcessingStatus: Array,
|
||||
newPayment: Array,
|
||||
@@ -65,9 +66,9 @@ const permitSchema = new mongoose.Schema({
|
||||
}).index({ tenantId: 1, permitNumber: 1 }, { unique: true });
|
||||
|
||||
export const permitFields = Object.keys(permitSchema.paths).filter(
|
||||
(path) => path !== "__v"
|
||||
(path) => path !== '__v'
|
||||
);
|
||||
export const permitModel = mongoose.model("permit", permitSchema);
|
||||
export const permitModel = mongoose.model('permit', permitSchema);
|
||||
|
||||
const permitCore = {
|
||||
permitNumber: z.string(),
|
||||
@@ -130,11 +131,11 @@ const listPermitResponse = z.object({
|
||||
|
||||
const updatePermitInput = z.object({
|
||||
manualStatus: z
|
||||
.enum(["Ready To Issue", "Issued", "Invoiced", "Paid", "Closed"])
|
||||
.enum(['Ready To Issue', 'Issued', 'Invoiced', 'Paid', 'Closed'])
|
||||
.nullable()
|
||||
.optional(),
|
||||
utility: z
|
||||
.enum(["Submitted", "Pending", "Applied", "Rejected", "Paid"])
|
||||
.enum(['Submitted', 'Pending', 'Applied', 'Rejected', 'Paid'])
|
||||
.nullable()
|
||||
.optional(),
|
||||
assignedTo: z.string().optional(),
|
||||
@@ -153,5 +154,5 @@ export const { schemas: permitSchemas, $ref: $permit } = buildJsonSchemas(
|
||||
updatePermitInput,
|
||||
pageQueryParams,
|
||||
},
|
||||
{ $id: "permit" }
|
||||
{ $id: 'permit' }
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user