Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cal.com & analytics #248

Merged
merged 25 commits into from
Mar 25, 2024
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
2 changes: 2 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"license": "MIT",
"scripts": {
"postinstall": "prisma generate",
"generate": "prisma generate",
"db:push": "prisma db push --accept-data-loss",
"db:migrate": "prisma migrate dev",
"build": "tsc",
Expand Down Expand Up @@ -35,6 +36,7 @@
"dotenv": "^16.0.0",
"fastify": "4.22.2",
"got": "^13.0.0",
"handlebars": "^4.7.8",
"i": "^0.3.7",
"imap": "^0.8.19",
"jsonwebtoken": "9.0.2",
Expand Down
77 changes: 76 additions & 1 deletion apps/api/src/controllers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Feature Flags

import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import nodeMailer from "nodemailer";

import { checkToken } from "../lib/jwt";
import { prisma } from "../prisma";
Expand Down Expand Up @@ -152,7 +153,15 @@ export function configRoutes(fastify: FastifyInstance) {

if (token) {
// GET EMAIL SETTINGS
const config = await prisma.email.findFirst({});
const config = await prisma.email.findFirst({
select: {
active: true,
host: true,
port: true,
reply: true,
user: true,
},
});

if (config === null) {
reply.send({
Expand Down Expand Up @@ -232,6 +241,72 @@ export function configRoutes(fastify: FastifyInstance) {
);

// Test email is working
fastify.get(
"/api/v1/config/email/verify",

async (request: FastifyRequest, reply: FastifyReply) => {
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

if (token) {
// GET EMAIL SETTINGS
const config = await prisma.email.findFirst({});

if (config === null) {
return reply.send({
success: true,
active: false,
});
}

const emails = await prisma.email.findMany();
const email = emails[0];

const mail = nodeMailer.createTransport({
// @ts-ignore
host: email.host,
port: email.port,
secure: email.port === "465" ? true : false,
auth: {
user: email.user,
pass: email.pass,
},
});

const ver = await mail.verify();

if (ver) {
reply.send({
success: true,
message: "Email is working!",
});
} else {
reply.send({
success: false,
message:
"Incorrect settings or credentials provided. Please check and try again.",
});
}
}
}
);

// Disable/Enable Email
fastify.delete(
"/api/v1/config/email",

async (request: FastifyRequest, reply: FastifyReply) => {
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

if (token) {
await prisma.email.deleteMany({});

reply.send({
success: true,
message: "Email settings deleted!",
});
}
}
);
}
89 changes: 82 additions & 7 deletions apps/api/src/controllers/ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,20 +240,15 @@ export function ticketRoutes(fastify: FastifyInstance) {
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

const { title }: any = request.body;
const { query }: any = request.body;

if (token) {
const tickets = await prisma.ticket.findMany({
where: {
title: {
contains: title,
contains: query,
},
},
orderBy: [
{
createdAt: "desc",
},
],
});

reply.send({
Expand Down Expand Up @@ -649,4 +644,84 @@ export function ticketRoutes(fastify: FastifyInstance) {

async (request: FastifyRequest, reply: FastifyReply) => {}
);

// GET all ticket templates
fastify.get(
"/api/v1/ticket/templates",

async (request: FastifyRequest, reply: FastifyReply) => {
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

if (token) {
const templates = await prisma.emailTemplate.findMany({
select: {
createdAt: true,
updatedAt: true,
type: true,
id: true,
},
});

reply.send({
success: true,
templates: templates,
});
}
}
);

// GET ticket template by ID
fastify.get(
"/api/v1/ticket/template/:id",

async (request: FastifyRequest, reply: FastifyReply) => {
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

const { id }: any = request.params;

if (token) {
const template = await prisma.emailTemplate.findMany({
where: {
id: id,
},
});

reply.send({
success: true,
template: template,
});
}
}
);

// PUT ticket template by ID
fastify.put(
"/api/v1/ticket/template/:id",

async (request: FastifyRequest, reply: FastifyReply) => {
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

const { id }: any = request.params;

const { html }: any = request.body;

if (token) {
await prisma.emailTemplate.update({
where: {
id: id,
},
data: {
html: html,
},
});

reply.send({
success: true,
});
}
}
);
}
2 changes: 1 addition & 1 deletion apps/api/src/lib/nodemailer/auth/forgot-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function forgotPassword(
// @ts-ignore
host: email.host,
port: email.port,
secure: email.secure, // true for 465, false for other ports
secure: email.port === "465" ? true : false, // true for 465, false for other ports
auth: {
user: email.user, // generated ethereal user
pass: email.pass, // generated ethereal password
Expand Down
59 changes: 16 additions & 43 deletions apps/api/src/lib/nodemailer/ticket/assigned.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import handlebars from "handlebars";
import nodeMailer from "nodemailer";
import { prisma } from "../../../prisma";

Expand All @@ -10,13 +11,13 @@ export async function sendAssignedEmail(email: any) {

if (emails.length > 0) {
if (process.env.ENVIRONMENT === "development") {
let testAccount = await nodeMailer.createTestAccount();
// let testAccount = await nodeMailer.createTestAccount();
mail = nodeMailer.createTransport({
host: "localhost",
port: 1025,
secure: false, // true for 465, false for other ports
auth: {
user: testAccount.user, // generated ethereal user
pass: testAccount.pass, // generated ethereal password
user: "project.1",
pass: "secret.1",
},
});
} else {
Expand All @@ -26,7 +27,7 @@ export async function sendAssignedEmail(email: any) {
// @ts-ignore
host: email.host,
port: email.port,
secure: email.secure, // true for 465, false for other ports
secure: email.port === "465" ? true : false, // true for 465, false for other ports
auth: {
user: email.user, // generated ethereal user
pass: email.pass, // generated ethereal password
Expand All @@ -36,50 +37,22 @@ export async function sendAssignedEmail(email: any) {

console.log("Sending email to: ", email);

const testhtml = await prisma.emailTemplate.findFirst({
where: {
type: "ticket_assigned",
},
});

var template = handlebars.compile(testhtml?.html);
var htmlToSend = template({}); // Pass an empty object as the argument to the template function

await mail
.sendMail({
from: replyto, // sender address
to: email, // list of receivers
subject: `A new ticket has been assigned to you`, // Subject line
text: `Hello there, a ticket has been assigned to you`, // plain text body
html: `
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8" />
</head>
<div id="" style="display:none;overflow:hidden;line-height:1px;opacity:0;max-height:0;max-width:0">Ticket Created<div></div>
</div>

<body style="background-color:#ffffff;margin:0 auto;font-family:-apple-system, BlinkMacSystemFont, &#x27;Segoe UI&#x27;, &#x27;Roboto&#x27;, &#x27;Oxygen&#x27;, &#x27;Ubuntu&#x27;, &#x27;Cantarell&#x27;, &#x27;Fira Sans&#x27;, &#x27;Droid Sans&#x27;, &#x27;Helvetica Neue&#x27;, sans-serif">
<table align="center" role="presentation" cellSpacing="0" cellPadding="0" border="0" width="100%" style="max-width:600px;margin:0 auto">
<tr style="width:100%">
<td>
<table style="margin-top:8px" align="center" border="0" cellPadding="0" cellSpacing="0" role="presentation" width="100%">
</table>
<h1 style="color:#1d1c1d;font-size:16px;font-weight:700;margin:10px 0;padding:0;line-height:42px">Ticket Assigned</h1>
<p style="font-size:20px;line-height:28px;margin:4px 0">
<p>Hello, <br>A new ticket has been assigned to you.</p>
<p style="font-size:14px;margin:16px 0;color:#000">
Kind regards,

<table align="center" border="0" cellPadding="0" cellSpacing="0" role="presentation" width="100%">
<tbody>
<tr>
<td>
<a target="_blank" style="color:#b7b7b7;text-decoration:underline" href="https://docs.peppermint.sh" rel="noopener noreferrer">Documentation</a> | <a target="_blank" style="color:#b7b7b7;text-decoration:underline" href="https://discord.gg/8XFkbgKmgv" rel="noopener noreferrer">Discord</a> </a>
<p style="font-size:12px;line-height:15px;margin:16px 0;color:#b7b7b7;text-align:left">This was an automated message sent by peppermint.sh -> An open source helpdesk solution</p>
<p style="font-size:12px;line-height:15px;margin:16px 0;color:#b7b7b7;text-align:left;margin-bottom:50px">©2022 Peppermint Ticket Management, a Peppermint Labs product.<br />All rights reserved.</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</body>
</html>
`,
html: htmlToSend,
})
.then((info) => {
console.log("Message sent: %s", info.messageId);
Expand Down
Loading
Loading