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