models/communities/JoinGroupQuery.js

import UserIdList from './../UserIdList.js';
import UpdateGroupMembersQuery from './UpdateGroupMembersQuery.js';
import MemberStatus from './MemberStatus.js';
import Role from './Role.js';
import UserId from '../UserId.js';

export default class JoinGroupQuery {
    /**
     * Creates a JoinGroupQuery.
     *
     * @constructor JoinGroupQuery
     * @param {UpdateGroupMembersQuery} query - Group Members Query
     */
    constructor(query) {
        this.internalQuery = query;
    }

    /**
    * Create query to add users to a group.
    *
    * @memberof JoinGroupQuery
    * @param {string} groupId - Group ID.
    * @return {JoinGroupQuery} New instance.
    */
    static create(groupId) {
        const currentUserId = UserId.currentUser();
        let uQuery = UpdateGroupMembersQuery
            .create(groupId, UserIdList.create([currentUserId.id]));
        uQuery = uQuery.withRole(Role.Member);
        uQuery = uQuery.withMemberStatus(MemberStatus.ApprovalPending);
        return new JoinGroupQuery(uQuery);
    }

    /**
     * Add invitation token.
     *
     * @memberof JoinGroupQuery
     * @instance
     * @param {string} token - Invitation token.
     * @return {JoinGroupQuery} Same instance.
     */
    withInvitationToken(token) {
        this.invitationToken = token;
        return this;
    }
}