external-user-group.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import type { Document, Model } from 'mongoose';
  2. import { Schema } from 'mongoose';
  3. import mongoosePaginate from 'mongoose-paginate-v2';
  4. import type { IExternalUserGroup } from '~/features/external-user-group/interfaces/external-user-group';
  5. import UserGroup from '~/server/models/user-group';
  6. import { getOrCreateModel } from '~/server/util/mongoose-utils';
  7. export interface ExternalUserGroupDocument
  8. extends IExternalUserGroup,
  9. Document {}
  10. export interface ExternalUserGroupModel
  11. extends Model<ExternalUserGroupDocument> {
  12. // biome-ignore lint/suspicious/noExplicitAny: ignore
  13. [x: string]: any; // for old methods
  14. PAGE_ITEMS: 10;
  15. findGroupsWithDescendantsRecursively: (
  16. groups: ExternalUserGroupDocument[],
  17. descendants?: ExternalUserGroupDocument[],
  18. ) => Promise<ExternalUserGroupDocument[]>;
  19. }
  20. const schema = new Schema<ExternalUserGroupDocument, ExternalUserGroupModel>(
  21. {
  22. name: { type: String, required: true },
  23. parent: {
  24. type: Schema.Types.ObjectId,
  25. ref: 'ExternalUserGroup',
  26. index: true,
  27. },
  28. description: { type: String, default: '' },
  29. externalId: { type: String, required: true, unique: true },
  30. provider: { type: String, required: true },
  31. },
  32. {
  33. timestamps: true,
  34. },
  35. );
  36. schema.plugin(mongoosePaginate);
  37. // group name should be unique for each provider
  38. schema.index({ name: 1, provider: 1 }, { unique: true });
  39. /**
  40. * Find group that has specified externalId and update, or create one if it doesn't exist.
  41. * @param name ExternalUserGroup name
  42. * @param name ExternalUserGroup externalId
  43. * @param name ExternalUserGroup provider
  44. * @param name ExternalUserGroup description
  45. * @param name ExternalUserGroup parentId
  46. * @returns ExternalUserGroupDocument[]
  47. */
  48. schema.statics.findAndUpdateOrCreateGroup = async function (
  49. name: string,
  50. externalId: string,
  51. provider: string,
  52. description?: string,
  53. parentId?: string,
  54. ) {
  55. let parent: ExternalUserGroupDocument | null = null;
  56. if (parentId != null) {
  57. parent = await this.findOne({ _id: parentId });
  58. if (parent == null) {
  59. throw Error('Parent does not exist.');
  60. }
  61. }
  62. return this.findOneAndUpdate(
  63. { externalId },
  64. {
  65. name,
  66. description,
  67. provider,
  68. parent,
  69. },
  70. { upsert: true, new: true },
  71. );
  72. };
  73. schema.statics.findWithPagination = UserGroup.findWithPagination;
  74. schema.statics.findChildrenByParentIds = UserGroup.findChildrenByParentIds;
  75. schema.statics.findGroupsWithAncestorsRecursively =
  76. UserGroup.findGroupsWithAncestorsRecursively;
  77. schema.statics.findGroupsWithDescendantsRecursively =
  78. UserGroup.findGroupsWithDescendantsRecursively;
  79. schema.statics.findGroupsWithDescendantsById =
  80. UserGroup.findGroupsWithDescendantsById;
  81. export default getOrCreateModel<
  82. ExternalUserGroupDocument,
  83. ExternalUserGroupModel
  84. >('ExternalUserGroup', schema);