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) {
|
||||
|
||||
// Create E-Mail Account
|
||||
server.post("/email/accounts/", async (req, reply) => {
|
||||
server.post("/email/accounts/:id?", async (req, reply) => {
|
||||
if (!req.user?.tenant_id) {
|
||||
return reply.code(400).send({ error: "No tenant selected" });
|
||||
}
|
||||
@@ -23,32 +23,64 @@ export default async function emailAsUserRoutes(server: FastifyInstance) {
|
||||
imap_ssl: boolean
|
||||
};
|
||||
|
||||
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,
|
||||
if(req.params.id) {
|
||||
//SAVE Existing
|
||||
let saveData = {
|
||||
email_encrypted: body.email ? encrypt(body.email) : undefined,
|
||||
password_encrypted: body.password ? encrypt(body.password) : undefined,
|
||||
smtp_host_encrypted: body.smtp_host ? encrypt(body.smtp_host) : undefined,
|
||||
smtp_port: body.smtp_port,
|
||||
smtp_ssl: body.smtp_ssl,
|
||||
imap_host_encrypted: body.imap_host ? encrypt(body.imap_host) : undefined,
|
||||
imap_port: body.imap_port,
|
||||
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) => {
|
||||
|
||||
Reference in New Issue
Block a user