Add sorting
This commit is contained in:
@@ -3,30 +3,32 @@ import mongoose from "mongoose";
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { pageMetadata, pageQueryParams } from "../pagination";
|
import { pageMetadata, pageQueryParams } from "../pagination";
|
||||||
|
|
||||||
export const orgModel = mongoose.model(
|
const orgSchema = new mongoose.Schema({
|
||||||
"organization",
|
tenantId: {
|
||||||
new mongoose.Schema({
|
|
||||||
tenantId: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
pid: {
|
|
||||||
type: String,
|
|
||||||
unique: true,
|
|
||||||
},
|
|
||||||
name: String,
|
|
||||||
domain: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
avatar: String,
|
|
||||||
type: String,
|
type: String,
|
||||||
isClient: Boolean,
|
required: true,
|
||||||
status: String,
|
},
|
||||||
createdAt: Date,
|
pid: {
|
||||||
createdBy: String,
|
type: String,
|
||||||
updatedAt: Date,
|
unique: true,
|
||||||
}).index({ tenantId: 1, domain: 1 }, { unique: true })
|
},
|
||||||
|
name: String,
|
||||||
|
domain: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
avatar: String,
|
||||||
|
type: String,
|
||||||
|
isClient: Boolean,
|
||||||
|
status: String,
|
||||||
|
createdAt: Date,
|
||||||
|
createdBy: String,
|
||||||
|
updatedAt: Date,
|
||||||
|
}).index({ tenantId: 1, domain: 1 }, { unique: true });
|
||||||
|
|
||||||
|
export const orgFields = Object.keys(orgSchema.paths).filter(
|
||||||
|
(path) => path !== "__v"
|
||||||
);
|
);
|
||||||
|
export const orgModel = mongoose.model("organization", orgSchema);
|
||||||
|
|
||||||
const orgCore = {
|
const orgCore = {
|
||||||
name: z.string().max(30),
|
name: z.string().max(30),
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { PageQueryParams } from "../pagination";
|
import { getSortObject, PageQueryParams } from "../pagination";
|
||||||
import { generateId } from "../utils/id";
|
import { generateId } from "../utils/id";
|
||||||
import {
|
import {
|
||||||
CreateOrgInput,
|
CreateOrgInput,
|
||||||
|
orgFields,
|
||||||
orgModel,
|
orgModel,
|
||||||
UpdateOrgInput,
|
UpdateOrgInput,
|
||||||
} from "./organization.schema";
|
} from "./organization.schema";
|
||||||
@@ -26,13 +27,18 @@ export async function getOrg(orgId: string, tenantId: string) {
|
|||||||
export async function listOrgs(params: PageQueryParams, tenantId: string) {
|
export async function listOrgs(params: PageQueryParams, tenantId: string) {
|
||||||
const page = params.page || 1;
|
const page = params.page || 1;
|
||||||
const pageSize = params.pageSize || 10;
|
const pageSize = params.pageSize || 10;
|
||||||
|
const sortObj = getSortObject(params, orgFields);
|
||||||
|
|
||||||
const orgs = await orgModel.aggregate([
|
const orgs = await orgModel.aggregate([
|
||||||
{ $match: { $and: [{ tenantId: tenantId }] } },
|
{ $match: { $and: [{ tenantId: tenantId }] } },
|
||||||
{
|
{
|
||||||
$facet: {
|
$facet: {
|
||||||
metadata: [{ $count: "count" }],
|
metadata: [{ $count: "count" }],
|
||||||
data: [{ $skip: (page - 1) * pageSize }, { $limit: pageSize }],
|
data: [
|
||||||
|
{ $skip: (page - 1) * pageSize },
|
||||||
|
{ $limit: pageSize },
|
||||||
|
{ $sort: sortObj },
|
||||||
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -9,6 +9,29 @@ export const pageMetadata = z.object({
|
|||||||
export const pageQueryParams = z.object({
|
export const pageQueryParams = z.object({
|
||||||
page: z.number().optional(),
|
page: z.number().optional(),
|
||||||
pageSize: z.number().optional(),
|
pageSize: z.number().optional(),
|
||||||
|
sort: z.string().optional(),
|
||||||
|
filter: z.string().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type PageQueryParams = z.infer<typeof pageQueryParams>;
|
export type PageQueryParams = z.infer<typeof pageQueryParams>;
|
||||||
|
|
||||||
|
export function getSortObject(
|
||||||
|
params: PageQueryParams,
|
||||||
|
validFields: Array<string>
|
||||||
|
) {
|
||||||
|
const sortObj: Record<string, 1 | -1> = {};
|
||||||
|
|
||||||
|
if (params.sort && params.sort != "") {
|
||||||
|
const sortOptions = params.sort.split(",");
|
||||||
|
sortOptions.forEach((item) => {
|
||||||
|
const order = item.startsWith("-") ? -1 : 1;
|
||||||
|
const field = item.replace("-", "").trim();
|
||||||
|
|
||||||
|
if (validFields.includes(field)) sortObj[field] = order;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.keys(sortObj).length == 0) sortObj["createdAt"] = -1;
|
||||||
|
|
||||||
|
return sortObj;
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,51 +3,53 @@ import mongoose from "mongoose";
|
|||||||
import { buildJsonSchemas } from "fastify-zod";
|
import { buildJsonSchemas } from "fastify-zod";
|
||||||
import { pageMetadata, pageQueryParams } from "../pagination";
|
import { pageMetadata, pageQueryParams } from "../pagination";
|
||||||
|
|
||||||
export const permitModel = mongoose.model(
|
const permitSchema = new mongoose.Schema({
|
||||||
"permit",
|
tenantId: {
|
||||||
new mongoose.Schema({
|
type: String,
|
||||||
tenantId: {
|
required: true,
|
||||||
type: String,
|
},
|
||||||
required: true,
|
pid: {
|
||||||
},
|
type: String,
|
||||||
pid: {
|
unique: true,
|
||||||
type: String,
|
},
|
||||||
unique: true,
|
permitNumber: String,
|
||||||
},
|
county: {
|
||||||
permitNumber: String,
|
type: mongoose.Types.ObjectId,
|
||||||
county: {
|
ref: "organization",
|
||||||
type: mongoose.Types.ObjectId,
|
},
|
||||||
ref: "organization",
|
client: {
|
||||||
},
|
type: mongoose.Types.ObjectId,
|
||||||
client: {
|
ref: "organization",
|
||||||
type: mongoose.Types.ObjectId,
|
},
|
||||||
ref: "organization",
|
permitDate: Date,
|
||||||
},
|
stage: String,
|
||||||
permitDate: Date,
|
status: String,
|
||||||
stage: String,
|
assignedTo: {
|
||||||
status: String,
|
type: mongoose.Types.ObjectId,
|
||||||
assignedTo: {
|
ref: "user",
|
||||||
type: mongoose.Types.ObjectId,
|
},
|
||||||
ref: "user",
|
link: String,
|
||||||
},
|
address: String,
|
||||||
link: String,
|
recordType: String,
|
||||||
address: String,
|
description: String,
|
||||||
recordType: String,
|
applicationDetails: Object,
|
||||||
description: String,
|
applicationInfo: Object,
|
||||||
applicationDetails: Object,
|
applicationInfoTable: Object,
|
||||||
applicationInfo: Object,
|
conditions: Array,
|
||||||
applicationInfoTable: Object,
|
ownerDetails: String,
|
||||||
conditions: Array,
|
parcelInfo: Object,
|
||||||
ownerDetails: String,
|
paymentData: Object,
|
||||||
parcelInfo: Object,
|
professionalsList: Array,
|
||||||
paymentData: Object,
|
inspections: Object,
|
||||||
professionalsList: Array,
|
createdAt: Date,
|
||||||
inspections: Object,
|
updatedAt: Date,
|
||||||
createdAt: Date,
|
createdBy: String,
|
||||||
updatedAt: Date,
|
}).index({ tenantId: 1, permitNumber: 1 }, { unique: true });
|
||||||
createdBy: String,
|
|
||||||
}).index({ tenantId: 1, permitNumber: 1 }, { unique: true })
|
export const permitFields = Object.keys(permitSchema.paths).filter(
|
||||||
|
(path) => path !== "__v"
|
||||||
);
|
);
|
||||||
|
export const permitModel = mongoose.model("permit", permitSchema);
|
||||||
|
|
||||||
const permitCore = {
|
const permitCore = {
|
||||||
permitNumber: z.string(),
|
permitNumber: z.string(),
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { PageQueryParams } from "../pagination";
|
import { getSortObject, PageQueryParams } from "../pagination";
|
||||||
import { generateId } from "../utils/id";
|
import { generateId } from "../utils/id";
|
||||||
import {
|
import {
|
||||||
CreatePermitInput,
|
CreatePermitInput,
|
||||||
|
permitFields,
|
||||||
permitModel,
|
permitModel,
|
||||||
UpdatePermitInput,
|
UpdatePermitInput,
|
||||||
} from "./permit.schema";
|
} from "./permit.schema";
|
||||||
@@ -29,6 +30,7 @@ export async function getPermit(permitId: string, tenantId: string) {
|
|||||||
export async function listPermits(params: PageQueryParams, tenantId: string) {
|
export async function listPermits(params: PageQueryParams, tenantId: string) {
|
||||||
const page = params.page || 1;
|
const page = params.page || 1;
|
||||||
const pageSize = params.pageSize || 10;
|
const pageSize = params.pageSize || 10;
|
||||||
|
const sortObj = getSortObject(params, permitFields);
|
||||||
|
|
||||||
const permitsList = await permitModel.aggregate([
|
const permitsList = await permitModel.aggregate([
|
||||||
{
|
{
|
||||||
@@ -74,7 +76,11 @@ export async function listPermits(params: PageQueryParams, tenantId: string) {
|
|||||||
{
|
{
|
||||||
$facet: {
|
$facet: {
|
||||||
metadata: [{ $count: "count" }],
|
metadata: [{ $count: "count" }],
|
||||||
data: [{ $skip: (page - 1) * pageSize }, { $limit: pageSize }],
|
data: [
|
||||||
|
{ $skip: (page - 1) * pageSize },
|
||||||
|
{ $limit: pageSize },
|
||||||
|
{ $sort: sortObj },
|
||||||
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user