Skip to content

Commit

Permalink
fix new orgs always broken on naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycarambat committed Dec 19, 2023
1 parent 25d18aa commit 6ec72e4
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 323 deletions.
18 changes: 6 additions & 12 deletions backend/models/organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,21 @@ const Organization = {
organization: null,
message: "No Organization name provided.",
};
var slug = slugify(orgName, { lower: true });

const validNameRegex = /^[a-zA-Z0-9]+$/;

// Validate orgName against the regular expression
if (!validNameRegex.test(orgName)) {
return {
organization: null,
message: "Organization name contains invalid characters. Only alphanumeric characters are allowed.",
};
}
// If the new name contains bad characters
// replace them with spaces and continue creation.
const newOrgName = orgName.replace(/[:\.,<>@]/, " ");
var slug = slugify(newOrgName, { lower: true });

const existingBySlug = await this.get({ slug });
if (!!existingBySlug) {
const slugSeed = Math.floor(10000000 + Math.random() * 90000000);
slug = slugify(`${orgName}-${slugSeed}`, { lower: true });
slug = slugify(`${newOrgName}-${slugSeed}`, { lower: true });
}

const organization = await prisma.organizations.create({
data: {
name: orgName,
name: newOrgName,
slug,
uuid: this.makeKey(),
},
Expand Down
Loading

0 comments on commit 6ec72e4

Please sign in to comment.