external-user-group.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import type { Model, Document } 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 extends IExternalUserGroup, Document {}
  8. export interface ExternalUserGroupModel extends Model<ExternalUserGroupDocument> {
  9. [x:string]: any, // for old methods
  10. PAGE_ITEMS: 10,
  11. findGroupsWithDescendantsRecursively: (
  12. groups: ExternalUserGroupDocument[], descendants?: ExternalUserGroupDocument[]
  13. ) => Promise<ExternalUserGroupDocument[]>,
  14. }
  15. const schema = new Schema<ExternalUserGroupDocument, ExternalUserGroupModel>({
  16. name: { type: String, required: true },
  17. parent: { type: Schema.Types.ObjectId, ref: 'ExternalUserGroup', index: true },
  18. description: { type: String, default: '' },
  19. externalId: { type: String, required: true, unique: true },
  20. provider: { type: String, required: true },
  21. }, {
  22. timestamps: true,
  23. });
  24. schema.plugin(mongoosePaginate);
  25. // group name should be unique for each provider
  26. schema.index({ name: 1, provider: 1 }, { unique: true });
  27. /**
  28. * Find group that has specified externalId and update, or create one if it doesn't exist.
  29. * @param name ExternalUserGroup name
  30. * @param name ExternalUserGroup externalId
  31. * @param name ExternalUserGroup provider
  32. * @param name ExternalUserGroup description
  33. * @param name ExternalUserGroup parentId
  34. * @returns ExternalUserGroupDocument[]
  35. */
  36. schema.statics.findAndUpdateOrCreateGroup = async function(name: string, externalId: string, provider: string, description?: string, parentId?: string) {
  37. let parent: ExternalUserGroupDocument | null = null;
  38. if (parentId != null) {
  39. parent = await this.findOne({ _id: parentId });
  40. if (parent == null) {
  41. throw Error('Parent does not exist.');
  42. }
  43. }
  44. return this.findOneAndUpdate({ externalId }, {
  45. name, description, provider, parent,
  46. }, { upsert: true, new: true });
  47. };
  48. schema.statics.findWithPagination = UserGroup.findWithPagination;
  49. schema.statics.findChildrenByParentIds = UserGroup.findChildrenByParentIds;
  50. schema.statics.findGroupsWithAncestorsRecursively = UserGroup.findGroupsWithAncestorsRecursively;
  51. schema.statics.findGroupsWithDescendantsRecursively = UserGroup.findGroupsWithDescendantsRecursively;
  52. schema.statics.findGroupsWithDescendantsById = UserGroup.findGroupsWithDescendantsById;
  53. export default getOrCreateModel<ExternalUserGroupDocument, ExternalUserGroupModel>('ExternalUserGroup', schema);