diff --git a/backend/src/database/repositories/organizationRepository.ts b/backend/src/database/repositories/organizationRepository.ts index a26eba1291..e79442203e 100644 --- a/backend/src/database/repositories/organizationRepository.ts +++ b/backend/src/database/repositories/organizationRepository.ts @@ -515,6 +515,35 @@ class OrganizationRepository { return record.get({ plain: true }) } + static async findByDomain(domain, options: IRepositoryOptions) { + const transaction = SequelizeRepository.getTransaction(options) + const currentTenant = SequelizeRepository.getCurrentTenant(options) + + // Check if organization exists + const organization = await options.database.organization.findOne({ + where: { + website: { + [Sequelize.Op.or]: [ + // Matches URLs having 'http://' or 'https://' + { [Sequelize.Op.iLike]: `%://${domain}` }, + // Matches URLs having 'www' + { [Sequelize.Op.iLike]: `%://www.${domain}` }, + // Matches URLs that doesn't have 'http://' or 'https://' and 'www' + { [Sequelize.Op.iLike]: `${domain}` }, + ], + }, + tenantId: currentTenant.id, + }, + transaction, + }) + + if (!organization) { + return null + } + + return organization.get({ plain: true }) + } + static async filterIdInTenant(id, options: IRepositoryOptions) { return lodash.get(await this.filterIdsInTenant([id], options), '[0]', null) } diff --git a/backend/src/services/memberService.ts b/backend/src/services/memberService.ts index 989c5c8200..77f97cc5cf 100644 --- a/backend/src/services/memberService.ts +++ b/backend/src/services/memberService.ts @@ -303,10 +303,10 @@ export default class MemberService extends LoggerBase { } } + // Collect IDs for relation + const organizations = [] // If organizations are sent if (data.organizations) { - // Collect IDs for relation - const organizations = [] for (const organization of data.organizations) { if (typeof organization === 'string' && validator.isUUID(organization)) { // If an ID was already sent, we simply push it to the list @@ -329,33 +329,35 @@ export default class MemberService extends LoggerBase { organizations.push({ id: organizationRecord.id }) } } + } - // Auto assign member to organization if email domain matches - if (data.emails) { - const emailDomains = new Set() + // Auto assign member to organization if email domain matches + if (data.emails) { + const emailDomains = new Set() - // Collect unique domains - for (const email of data.emails) { - if (!email) { - continue - } - const domain = email.split('@')[1] - emailDomains.add(domain) + // Collect unique domains + for (const email of data.emails) { + if (!email) { + continue } + const domain = email.split('@')[1] + emailDomains.add(domain) + } - // Fetch organization ids for these domains - const organizationService = new OrganizationService(this.options) - for (const domain of emailDomains) { - if (domain) { - const organizationRecord = await organizationService.findByUrl(domain) - if (organizationRecord) { - organizations.push({ id: organizationRecord.id }) - } + // Fetch organization ids for these domains + const organizationService = new OrganizationService(this.options) + for (const domain of emailDomains) { + if (domain) { + const organizationRecord = await organizationService.findByDomain(domain) + if (organizationRecord) { + organizations.push({ id: organizationRecord.id }) } } } + } - // Remove dups + // Remove dups + if (organizations.length > 0) { data.organizations = lodash.uniqBy(organizations, 'id') } diff --git a/backend/src/services/organizationService.ts b/backend/src/services/organizationService.ts index fe2669cbfd..fd1775c865 100644 --- a/backend/src/services/organizationService.ts +++ b/backend/src/services/organizationService.ts @@ -212,6 +212,10 @@ export default class OrganizationService extends LoggerBase { return OrganizationRepository.findByUrl(url, this.options) } + async findByDomain(domain) { + return OrganizationRepository.findByDomain(domain, this.options) + } + async query(data) { const advancedFilter = data.filter const orderBy = data.orderBy