page-operation.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import mongoose, {
  2. Schema, Model, Document, QueryOptions, FilterQuery,
  3. } from 'mongoose';
  4. import { getOrCreateModel } from '@growi/core';
  5. import {
  6. IPageForResuming, IUserForResuming, IOptionsForResuming,
  7. } from '~/server/interfaces/page-operation';
  8. import { ObjectIdLike } from '../interfaces/mongoose-utils';
  9. type IObjectId = mongoose.Types.ObjectId;
  10. const ObjectId = mongoose.Schema.Types.ObjectId;
  11. export const PageActionType = {
  12. Rename: 'Rename',
  13. Duplicate: 'Duplicate',
  14. Delete: 'Delete',
  15. DeleteCompletely: 'DeleteCompletely',
  16. Revert: 'Revert',
  17. NormalizeParent: 'NormalizeParent',
  18. } as const;
  19. export type PageActionType = typeof PageActionType[keyof typeof PageActionType];
  20. export const PageActionStage = {
  21. Main: 'Main',
  22. Sub: 'Sub',
  23. } as const;
  24. export type PageActionStage = typeof PageActionStage[keyof typeof PageActionStage];
  25. /*
  26. * Main Schema
  27. */
  28. export interface IPageOperation {
  29. actionType: PageActionType,
  30. actionStage: PageActionStage,
  31. fromPath: string,
  32. toPath?: string,
  33. page: IPageForResuming,
  34. user: IUserForResuming,
  35. options?: IOptionsForResuming,
  36. incForUpdatingDescendantCount?: number,
  37. }
  38. export interface PageOperationDocument extends IPageOperation, Document {}
  39. export type PageOperationDocumentHasId = PageOperationDocument & { _id: ObjectIdLike };
  40. export interface PageOperationModel extends Model<PageOperationDocument> {
  41. findByIdAndUpdatePageActionStage(pageOpId: ObjectIdLike, stage: PageActionStage): Promise<PageOperationDocumentHasId | null>
  42. findMainOps(filter?: FilterQuery<PageOperationDocument>, projection?: any, options?: QueryOptions): Promise<PageOperationDocumentHasId[]>
  43. }
  44. const pageSchemaForResuming = new Schema<IPageForResuming>({
  45. _id: { type: ObjectId, ref: 'Page', index: true },
  46. parent: { type: ObjectId, ref: 'Page' },
  47. descendantCount: { type: Number },
  48. isEmpty: { type: Boolean },
  49. path: { type: String, required: true, index: true },
  50. revision: { type: ObjectId, ref: 'Revision' },
  51. status: { type: String },
  52. grant: { type: Number },
  53. grantedUsers: [{ type: ObjectId, ref: 'User' }],
  54. grantedGroup: { type: ObjectId, ref: 'UserGroup' },
  55. creator: { type: ObjectId, ref: 'User' },
  56. lastUpdateUser: { type: ObjectId, ref: 'User' },
  57. });
  58. const userSchemaForResuming = new Schema<IUserForResuming>({
  59. _id: { type: ObjectId, ref: 'User', required: true },
  60. });
  61. const optionsSchemaForResuming = new Schema<IOptionsForResuming>({
  62. createRedirectPage: { type: Boolean },
  63. updateMetadata: { type: Boolean },
  64. prevDescendantCount: { type: Number },
  65. }, { _id: false });
  66. const schema = new Schema<PageOperationDocument, PageOperationModel>({
  67. actionType: {
  68. type: String,
  69. enum: PageActionType,
  70. required: true,
  71. index: true,
  72. },
  73. actionStage: {
  74. type: String,
  75. enum: PageActionStage,
  76. required: true,
  77. index: true,
  78. },
  79. fromPath: { type: String, required: true, index: true },
  80. toPath: { type: String, index: true },
  81. page: { type: pageSchemaForResuming, required: true },
  82. user: { type: userSchemaForResuming, required: true },
  83. options: { type: optionsSchemaForResuming },
  84. incForUpdatingDescendantCount: { type: Number },
  85. });
  86. schema.statics.findByIdAndUpdatePageActionStage = async function(
  87. pageOpId: ObjectIdLike, stage: PageActionStage,
  88. ): Promise<PageOperationDocumentHasId | null> {
  89. return this.findByIdAndUpdate(pageOpId, {
  90. $set: { actionStage: stage },
  91. }, { new: true });
  92. };
  93. schema.statics.findMainOps = async function(
  94. filter?: FilterQuery<PageOperationDocument>, projection?: any, options?: QueryOptions,
  95. ): Promise<PageOperationDocumentHasId[]> {
  96. return this.find(
  97. { ...filter, actionStage: PageActionStage.Main },
  98. projection,
  99. options,
  100. );
  101. };
  102. export default getOrCreateModel<PageOperationDocument, PageOperationModel>('PageOperation', schema);