Added E-Mail Account Saving
This commit is contained in:
@@ -7,7 +7,7 @@ import {secrets} from "../utils/secrets";
|
|||||||
export default async function emailAsUserRoutes(server: FastifyInstance) {
|
export default async function emailAsUserRoutes(server: FastifyInstance) {
|
||||||
|
|
||||||
// Create E-Mail Account
|
// Create E-Mail Account
|
||||||
server.post("/email/accounts/", async (req, reply) => {
|
server.post("/email/accounts/:id?", async (req, reply) => {
|
||||||
if (!req.user?.tenant_id) {
|
if (!req.user?.tenant_id) {
|
||||||
return reply.code(400).send({ error: "No tenant selected" });
|
return reply.code(400).send({ error: "No tenant selected" });
|
||||||
}
|
}
|
||||||
@@ -23,32 +23,64 @@ export default async function emailAsUserRoutes(server: FastifyInstance) {
|
|||||||
imap_ssl: boolean
|
imap_ssl: boolean
|
||||||
};
|
};
|
||||||
|
|
||||||
let createData = {
|
if(req.params.id) {
|
||||||
user_id: req.user.user_id,
|
//SAVE Existing
|
||||||
email_encrypted: encrypt(body.email),
|
let saveData = {
|
||||||
password_encrypted: encrypt(body.password),
|
email_encrypted: body.email ? encrypt(body.email) : undefined,
|
||||||
tenant_id: req.user.tenant_id,
|
password_encrypted: body.password ? encrypt(body.password) : undefined,
|
||||||
smtp_host_encrypted: encrypt(body.smtp_host),
|
smtp_host_encrypted: body.smtp_host ? encrypt(body.smtp_host) : undefined,
|
||||||
smtp_port: body.smtp_port,
|
smtp_port: body.smtp_port,
|
||||||
smtp_ssl: body.smtp_ssl,
|
smtp_ssl: body.smtp_ssl,
|
||||||
type: "mail",
|
imap_host_encrypted: body.imap_host ? encrypt(body.imap_host) : undefined,
|
||||||
imap_host_encrypted: encrypt(body.imap_host),
|
imap_port: body.imap_port,
|
||||||
imap_port: body.imap_port,
|
imap_ssl: body.imap_ssl,
|
||||||
imap_ssl: body.imap_ssl,
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const { data, error } = await server.supabase
|
||||||
|
.from("user_credentials")
|
||||||
|
.update(saveData)
|
||||||
|
.eq("id", req.params.id)
|
||||||
|
.select("*")
|
||||||
|
.single();
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return reply.code(400).send({ error: error.message });
|
||||||
|
} else {
|
||||||
|
return reply.send({success: true})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//Create New
|
||||||
|
let createData = {
|
||||||
|
user_id: req.user.user_id,
|
||||||
|
email_encrypted: encrypt(body.email),
|
||||||
|
password_encrypted: encrypt(body.password),
|
||||||
|
tenant_id: req.user.tenant_id,
|
||||||
|
smtp_host_encrypted: encrypt(body.smtp_host),
|
||||||
|
smtp_port: body.smtp_port,
|
||||||
|
smtp_ssl: body.smtp_ssl,
|
||||||
|
type: "mail",
|
||||||
|
imap_host_encrypted: encrypt(body.imap_host),
|
||||||
|
imap_port: body.imap_port,
|
||||||
|
imap_ssl: body.imap_ssl,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const { data, error } = await server.supabase
|
||||||
|
.from("user_credentials")
|
||||||
|
.insert(createData)
|
||||||
|
.select("*")
|
||||||
|
.single();
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return reply.code(400).send({ error: error.message });
|
||||||
|
} else {
|
||||||
|
return reply.send({success: true})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const { data, error } = await server.supabase
|
|
||||||
.from("user_credentials")
|
|
||||||
.insert(createData)
|
|
||||||
.select("*")
|
|
||||||
.single();
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
return reply.code(400).send({ error: error.message });
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
server.get("/email/accounts/:id?", async (req, reply) => {
|
server.get("/email/accounts/:id?", async (req, reply) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user