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
5 changes: 3 additions & 2 deletions api/controllers/InboxController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function createInbox({
? new mongoose.Types.ObjectId(domain_id)
: undefined;
inbox.name = name;
inbox.email = email || `${name.replace(/[^a-zA-Z0-9]/g, "-")}@${process.env.DEFAULT_EMAIL_DOMAIN}`;
inbox.email = email || await getNewRandomInboxEmail({ name });
await inbox.save();
return inbox;
}
Expand Down Expand Up @@ -60,7 +60,8 @@ export async function getInboxByEmail(email: string) {
}

export async function getNewRandomInboxEmail({ name }: { name: string }) {
const email = `${name.replace(/[^a-zA-Z0-9]/g, "-")}-${Math.random()
const lowerCaseName = name.toLowerCase();
const email = `${lowerCaseName.replace(/[^a-zA-Z0-9]/g, "-")}-${Math.random()
.toString(36)
.substring(2, 12)}@${process.env.DEFAULT_EMAIL_DOMAIN}`;
const inbox = await getInboxByEmail(email);
Expand Down
23 changes: 19 additions & 4 deletions api/routes/v1/inboxes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import { expressValidatorMiddleware } from "../../../middlewares/expressValidato
import {
createInbox,
deleteInboxByOrganizationIdAndInboxId,
getInboxByEmail,
getInboxByOrganizationIdAndInboxId,
getInboxesByOrganizationId,
getNewRandomInboxEmail,
} from "../../../controllers/InboxController";
import { sendWebhookEvent } from "../../../controllers/WebhookAttemptController";
import messagesRouter from "./messages";
Expand Down Expand Up @@ -40,7 +42,9 @@ router.post(
res: Response
) => {
let domainId: string | undefined;
let username: string | undefined;
if (req.body.email) {
username = req.body.email?.split("@")[0];
const domainName = req.body.email?.split("@")[1];
if (!domainName) {
return res.status(400).json({ error: "Invalid email address" });
Expand All @@ -49,18 +53,29 @@ router.post(
organizationId: req.organization._id.toString(),
name: domainName,
});
if (!domain) {
return res.status(404).json({ error: "Domain not found" });

if (domain) {
domainId = domain._id.toString();
}
}

domainId = domain._id.toString();
let email = undefined;
if (domainId) {
email = req.body.email;
} else if (username) {
const defaultEmail = `${username}@${process.env.DEFAULT_EMAIL_DOMAIN}`;
if (await getInboxByEmail(defaultEmail)) {
email = await getNewRandomInboxEmail({ name: username });
} else {
email = defaultEmail;
}
}

const inbox = await createInbox({
organization_id: req.organization._id.toString(),
name: req.body.name,
domain_id: domainId,
email: domainId && req.body.email,
email,
});

await sendWebhookEvent({
Expand Down
2 changes: 1 addition & 1 deletion app/app/pages/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

<p class="auth-footer">
New to Sendook?
<NuxtLink to="https://app.sendook.com/signup">Create an account</NuxtLink>
<NuxtLink to="/signup">Create an account</NuxtLink>
</p>
</div>
</template>
Expand Down