| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- const debug = require('debug')('growi:models:userGroupRelation');
- const mongoose = require('mongoose');
- const mongoosePaginate = require('mongoose-paginate');
- const ObjectId = mongoose.Schema.Types.ObjectId;
- /*
- * define schema
- */
- const schema = new mongoose.Schema({
- relatedGroup: { type: ObjectId, ref: 'UserGroup', required: true },
- relatedUser: { type: ObjectId, ref: 'User', required: true },
- createdAt: { type: Date, default: Date.now, required: true },
- });
- schema.plugin(mongoosePaginate);
- /**
- * UserGroupRelation Class
- *
- * @class UserGroupRelation
- */
- class UserGroupRelation {
- /**
- * limit items num for pagination
- *
- * @readonly
- * @static
- * @memberof UserGroupRelation
- */
- static get PAGE_ITEMS() {
- return 50;
- }
- static set crowi(crowi) {
- this._crowi = crowi;
- }
- static get crowi() {
- return this._crowi;
- }
- /**
- * find all user and group relation
- *
- * @static
- * @returns {Promise<UserGroupRelation[]>}
- * @memberof UserGroupRelation
- */
- static findAllRelation() {
- return this
- .find()
- .populate('relatedUser')
- .populate('relatedGroup')
- .exec();
- }
- /**
- * find all user and group relation of UserGroup
- *
- * @static
- * @param {UserGroup} userGroup
- * @returns {Promise<UserGroupRelation[]>}
- * @memberof UserGroupRelation
- */
- static findAllRelationForUserGroup(userGroup) {
- debug('findAllRelationForUserGroup is called', userGroup);
- var UserGroupRelation = this;
- return this
- .find({ relatedGroup: userGroup })
- .populate('relatedUser')
- .exec();
- }
- /**
- * find all user and group relation of UserGroups
- *
- * @static
- * @param {UserGroup[]} userGroups
- * @returns {Promise<UserGroupRelation[]>}
- * @memberof UserGroupRelation
- */
- static findAllRelationForUserGroups(userGroups) {
- return this
- .find({ relatedGroup: { $in: userGroups } })
- .populate('relatedUser')
- .exec();
- }
- /**
- * find all user and group relation of User
- *
- * @static
- * @param {User} user
- * @returns {Promise<UserGroupRelation[]>}
- * @memberof UserGroupRelation
- */
- static findAllRelationForUser(user) {
- return this
- .find({ relatedUser: user.id })
- .populate('relatedGroup')
- .exec();
- }
- /**
- * find all entities with pagination
- *
- * @see https://github.com/edwardhotchkiss/mongoose-paginate
- *
- * @static
- * @param {UserGroup} userGroup
- * @param {any} opts mongoose-paginate options object
- * @returns {Promise<any>} mongoose-paginate result object
- * @memberof UserGroupRelation
- */
- static findUserGroupRelationsWithPagination(userGroup, opts) {
- const query = { relatedGroup: userGroup };
- const options = Object.assign({}, opts);
- if (options.page == null) {
- options.page = 1;
- }
- if (options.limit == null) {
- options.limit = UserGroupRelation.PAGE_ITEMS;
- }
- return this.paginate(query, options)
- .catch((err) => {
- debug('Error on pagination:', err);
- });
- }
- /**
- * find one result by related group id and related user
- *
- * @static
- * @param {string} userGroupId find query param for relatedGroup
- * @param {User} userData find query param for relatedUser
- * @returns {Promise<UserGroupRelation>}
- * @memberof UserGroupRelation
- */
- static findByGroupIdAndUser(userGroupId, userData) {
- const query = {
- relatedGroup: userGroupId,
- relatedUser: userData.id
- };
- return this
- .findOne(query)
- .populate('relatedUser')
- .populate('relatedGroup')
- .exec();
- }
- /**
- * find all "not" related user for UserGroup
- *
- * @static
- * @param {UserGroup} userGroup for find users not related
- * @returns {Promise<User>}
- * @memberof UserGroupRelation
- */
- static findUserByNotRelatedGroup(userGroup) {
- const User = UserGroupRelation.crowi.model('User');
- return this.findAllRelationForUserGroup(userGroup)
- .then((relations) => {
- const relatedUserIds = relations.map((relation) => {
- return relation.relatedUser.id;
- });
- const query = { _id: { $nin: relatedUserIds }, status: User.STATUS_ACTIVE };
- debug('findUserByNotRelatedGroup ', query);
- return User.find(query).exec();
- });
- }
- /**
- * get if the user has relation for group
- *
- * @static
- * @param {User} userData
- * @param {UserGroup} userGroup
- * @returns {Promise<boolean>} is user related for group(or not)
- * @memberof UserGroupRelation
- */
- static isRelatedUserForGroup(userData, userGroup) {
- const query = {
- relatedGroup: userGroup.id,
- relatedUser: userData.id
- };
- return this
- .count(query)
- .exec()
- .then((count) => {
- // return true or false of the relation is exists(not count)
- return (0 < count);
- })
- .catch((err) => {
- debug('An Error occured.', err);
- reject(err);
- });
- }
- /**
- * create user and group relation
- *
- * @static
- * @param {UserGroup} userGroup
- * @param {User} user
- * @returns {Promise<UserGroupRelation>} created relation
- * @memberof UserGroupRelation
- */
- static createRelation(userGroup, user) {
- return this.create({
- relatedGroup: userGroup.id,
- relatedUser: user.id
- });
- }
- /**
- * remove all relation for UserGroup
- *
- * @static
- * @param {UserGroup} userGroup related group for remove
- * @returns {Promise<any>}
- * @memberof UserGroupRelation
- */
- static removeAllByUserGroup(userGroup) {
- return this.findAllRelationForUserGroup(userGroup)
- .then((relations) => {
- if (relations == null) {
- return;
- }
- else {
- relations.map((relation) => {
- relation.remove();
- });
- }
- });
- }
- /**
- * remove relation by id
- *
- * @static
- * @param {ObjectId} id
- * @returns {Promise<any>}
- * @memberof UserGroupRelation
- */
- static removeById(id) {
- return this.findById(id)
- .then((relationData) => {
- if (relationData == null) {
- throw new Exception('UserGroupRelation data is not exists. id:', id);
- }
- else {
- relationData.remove();
- }
- })
- .catch((err) => {
- debug('Error on find a removing user-group-relation', err);
- reject(err);
- });
- }
- }
- module.exports = function(crowi) {
- UserGroupRelation.crowi = crowi;
- schema.loadClass(UserGroupRelation);
- return mongoose.model('UserGroupRelation', schema);
- };
|