Added username invite

This commit is contained in:
Keith Holliday
2018-08-16 17:40:53 -05:00
parent 2a7dfff88a
commit f049d29d1b
6 changed files with 342 additions and 174 deletions

View File

@@ -356,23 +356,17 @@ schema.statics.toJSONCleanChat = async function groupToJSONCleanChat (group, use
return toJSON;
};
/**
* Checks invitation uuids and emails for possible errors.
*
* @param uuids An array of user ids
* @param emails An array of emails
* @param res Express res object for use with translations
* @throws BadRequest An error describing the issue with the invitations
*/
schema.statics.validateInvitations = async function getInvitationError (uuids, emails, res, group = null) {
let uuidsIsArray = Array.isArray(uuids);
let emailsIsArray = Array.isArray(emails);
let emptyEmails = emailsIsArray && emails.length < 1;
let emptyUuids = uuidsIsArray && uuids.length < 1;
function getIniviteError (uuids, emails, usernames) {
const uuidsIsArray = Array.isArray(uuids);
const emailsIsArray = Array.isArray(emails);
const usernamesIsArray = Array.isArray(usernames);
const emptyEmails = emailsIsArray && emails.length < 1;
const emptyUuids = uuidsIsArray && uuids.length < 1;
const emptyUsernames = usernamesIsArray && usernames.length < 1;
let errorString;
if (!uuids && !emails) {
if (!uuids && !emails && !usernames) {
errorString = 'canOnlyInviteEmailUuid';
} else if (uuids && !uuidsIsArray) {
errorString = 'uuidsMustBeAnArray';
@@ -386,10 +380,12 @@ schema.statics.validateInvitations = async function getInvitationError (uuids, e
errorString = 'inviteMustNotBeEmpty';
}
if (errorString) {
throw new BadRequest(res.t(errorString));
}
if (usernames && emptyUsernames) errorString = 'usernamesMustNotBeEmpty';
return errorString;
}
function getInviteCount (uuids, emails) {
let totalInvites = 0;
if (uuids) {
@@ -400,6 +396,28 @@ schema.statics.validateInvitations = async function getInvitationError (uuids, e
totalInvites += emails.length;
}
return totalInvites;
}
/**
* Checks invitation uuids and emails for possible errors.
*
* @param uuids An array of user ids
* @param emails An array of emails
* @param res Express res object for use with translations
* @throws BadRequest An error describing the issue with the invitations
*/
schema.statics.validateInvitations = async function getInvitationError (invites, res, group = null) {
const {
uuids,
emails,
usernames,
} = invites;
const errorString = getIniviteError(uuids, emails, usernames);
if (errorString) throw new BadRequest(res.t(errorString));
const totalInvites = getInviteCount(uuids, emails);
if (totalInvites > INVITES_LIMIT) {
throw new BadRequest(res.t('canOnlyInviteMaxInvites', {maxInvites: INVITES_LIMIT}));
}