Add sorting
This commit is contained in:
@@ -3,30 +3,32 @@ import mongoose from "mongoose";
|
||||
import { z } from "zod";
|
||||
import { pageMetadata, pageQueryParams } from "../pagination";
|
||||
|
||||
export const orgModel = mongoose.model(
|
||||
"organization",
|
||||
new mongoose.Schema({
|
||||
tenantId: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
pid: {
|
||||
type: String,
|
||||
unique: true,
|
||||
},
|
||||
name: String,
|
||||
domain: {
|
||||
type: String,
|
||||
},
|
||||
avatar: String,
|
||||
const orgSchema = new mongoose.Schema({
|
||||
tenantId: {
|
||||
type: String,
|
||||
isClient: Boolean,
|
||||
status: String,
|
||||
createdAt: Date,
|
||||
createdBy: String,
|
||||
updatedAt: Date,
|
||||
}).index({ tenantId: 1, domain: 1 }, { unique: true })
|
||||
required: true,
|
||||
},
|
||||
pid: {
|
||||
type: String,
|
||||
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 = {
|
||||
name: z.string().max(30),
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { PageQueryParams } from "../pagination";
|
||||
import { getSortObject, PageQueryParams } from "../pagination";
|
||||
import { generateId } from "../utils/id";
|
||||
import {
|
||||
CreateOrgInput,
|
||||
orgFields,
|
||||
orgModel,
|
||||
UpdateOrgInput,
|
||||
} from "./organization.schema";
|
||||
@@ -26,13 +27,18 @@ export async function getOrg(orgId: string, tenantId: string) {
|
||||
export async function listOrgs(params: PageQueryParams, tenantId: string) {
|
||||
const page = params.page || 1;
|
||||
const pageSize = params.pageSize || 10;
|
||||
const sortObj = getSortObject(params, orgFields);
|
||||
|
||||
const orgs = await orgModel.aggregate([
|
||||
{ $match: { $and: [{ tenantId: tenantId }] } },
|
||||
{
|
||||
$facet: {
|
||||
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