diff --git a/package.json b/package.json index 8c52437..4f3a6c9 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "@types/jest": "^26.0.4", "@types/moment": "^2.13.0", "@types/mongoose": "^5.7.28", + "@types/mongoose-paginate-v2": "^1.3.8", "@types/multer": "^1.4.3", "@types/multer-s3": "^2.7.7", "@types/mustache": "^4.0.1", @@ -32,6 +33,7 @@ "jest": "^26.1.0", "moment": "^2.27.0", "mongoose": "^5.9.20", + "mongoose-paginate-v2": "^1.3.9", "multer": "^1.4.2", "multer-s3": "^2.9.0", "multer-s3-v2": "^2.2.1", diff --git a/src/controllers/AdminOrganizationController.ts b/src/controllers/AdminOrganizationController.ts index bba1c2d..f4f972e 100644 --- a/src/controllers/AdminOrganizationController.ts +++ b/src/controllers/AdminOrganizationController.ts @@ -10,7 +10,33 @@ import Papa from 'papaparse' export default class AdminOrganizationController { static getMany(req: express.Request, res: express.Response) { - Organization.find().then(data => res.json({ success: true, data })) + //const limit: number = req.query.limit == null ? '1' : req.query.limit + let limit: any = req.query.limit == null ? '10' : req.query.limit + limit = parseInt(limit) + let page: any = req.query.page == null ? '1' : req.query.page + page = parseInt(page) + let paginationOptions: any = {} + console.log(page, limit) + if (limit !== -1) { + paginationOptions = { + page, limit + } + } else { + paginationOptions = { pagination: false } + } + let sortBy: any = req.query.sortBy == null ? 'validationState' : req.query.sortBy + let sortWay: any = req.query.sortDesc == null ? 1 : (req.query.sortDesc == 'true' ? -1 : 1) + let sort: any = {} + sort[sortBy] = sortWay + console.log(sort, paginationOptions) + Organization.paginate({}, { + sort, + ...paginationOptions + }).then(data => res.json({ + success: true, + data + }) + ) } static getOne(req: express.Request, res: express.Response) { @@ -204,6 +230,7 @@ export default class AdminOrganizationController { * @param res */ static approve(req: express.Request, res: express.Response) { + // (data.get('validationState') !== 'pending' && req.body.force == null) Organization.findById(req.params.id).then(data => { if ( data === null || diff --git a/src/models/Organization.ts b/src/models/Organization.ts index 3ee658a..648b8d7 100644 --- a/src/models/Organization.ts +++ b/src/models/Organization.ts @@ -1,5 +1,5 @@ import mongoose, { Schema } from 'mongoose' -import { Tag } from './Tag' +import mongoosePaginate from 'mongoose-paginate-v2' interface AllowedStringOptions { name: string; @@ -111,4 +111,6 @@ const Organization = new Schema({ publishedAt: { type: Date }, }) +Organization.plugin(mongoosePaginate) + export default mongoose.model('Organization', Organization) \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 5b7215d..5c910c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -698,6 +698,21 @@ "@types/bson" "*" "@types/node" "*" +"@types/mongoose-paginate-v2@^1.3.8": + version "1.3.8" + resolved "https://registry.yarnpkg.com/@types/mongoose-paginate-v2/-/mongoose-paginate-v2-1.3.8.tgz#e3eeb9817b5663bc62e5fec1aea9bb8df5482acc" + integrity sha512-76dmeOZ4//m0KvNINamY5zkYfEJPIVY8zK0HLXU+5E/h+v4ynQMA5z4hOJiCFUVIO50vENBrnQDQ2oC2RXqDEg== + dependencies: + "@types/mongoose" "*" + +"@types/mongoose@*": + version "5.7.36" + resolved "https://registry.yarnpkg.com/@types/mongoose/-/mongoose-5.7.36.tgz#2dae28c63041c6afba8a83ea02969f463b3f1021" + integrity sha512-ggFXgvkHgCNlT35B9d/heDYfSqOSwTmQjkRoR32sObGV5Xjd0N0WWuYlLzqeCg94j4hYN/OZxZ1VNNLltX/IVQ== + dependencies: + "@types/mongodb" "*" + "@types/node" "*" + "@types/mongoose@^5.7.28": version "5.7.28" resolved "https://registry.yarnpkg.com/@types/mongoose/-/mongoose-5.7.28.tgz#99385892962e51649d3a40912f2a6f2629558d89" @@ -4530,6 +4545,11 @@ mongoose-legacy-pluralize@1.0.2: resolved "https://registry.yarnpkg.com/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz#3ba9f91fa507b5186d399fb40854bff18fb563e4" integrity sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ== +mongoose-paginate-v2@^1.3.9: + version "1.3.9" + resolved "https://registry.yarnpkg.com/mongoose-paginate-v2/-/mongoose-paginate-v2-1.3.9.tgz#dc0f58c22e061d78fc3a898195b884870a737c54" + integrity sha512-KXLmsTYDaS7zHqT45B2MZcCGzJtBySGANor5Xf6c0nU3y34xkRMqcDiVTizLd27KGqy5smqLe6LVNkTK994XGA== + mongoose@^5.9.20: version "5.9.20" resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-5.9.20.tgz#975255196104ce760386ab439b1dad43caae23b5"