Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions backend/src/database/repositories/organizationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
44 changes: 23 additions & 21 deletions backend/src/services/memberService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
}

Expand Down
4 changes: 4 additions & 0 deletions backend/src/services/organizationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down