Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Peppermint-Lab/peppermint
Browse files Browse the repository at this point in the history
  • Loading branch information
potts99 committed Nov 9, 2024
2 parents 74ae049 + f01c5ca commit 5605536
Show file tree
Hide file tree
Showing 34 changed files with 1,935 additions and 415 deletions.
2 changes: 2 additions & 0 deletions apps/api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,5 @@ dist
.tern-port

/uploads

logs.log
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"mailparser": "^3.6.5",
"nodemailer": "^6.9.7",
"openid-client": "^5.7.0",
"pino": "^9.5.0",
"posthog-node": "^3.1.3",
"prisma": "5.6.0",
"samlify": "^2.8.11",
Expand Down
24 changes: 20 additions & 4 deletions apps/api/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ function generateRandomPassword(length: number): string {
return password;
}

async function tracking(event: string, properties: any) {
const client = track();

client.capture({
event: event,
properties: properties,
distinctId: "uuid",
});
}

export function authRoutes(fastify: FastifyInstance) {
// Register a new user
fastify.post(
Expand Down Expand Up @@ -175,7 +185,7 @@ export function authRoutes(fastify: FastifyInstance) {
const hog = track();

hog.capture({
event: "user_registered",
event: "user_registered_external",
distinctId: user.id,
});

Expand Down Expand Up @@ -339,6 +349,8 @@ export function authRoutes(fastify: FastifyInstance) {
},
});

await tracking("user_logged_in_password", {});

const data = {
id: user!.id,
email: user!.email,
Expand Down Expand Up @@ -523,13 +535,11 @@ export function authRoutes(fastify: FastifyInstance) {
// Retrieve user information
const userInfo = await oidcClient.userinfo(tokens.access_token);

console.log(userInfo);

let user = await prisma.user.findUnique({
where: { email: userInfo.email },
});

console.log(user);
await tracking("user_logged_in_oidc", {});

if (!user) {
// Create a new basic user
Expand Down Expand Up @@ -665,6 +675,8 @@ export function authRoutes(fastify: FastifyInstance) {
},
});

await tracking("user_logged_in_oauth", {});

// Send Response
reply.send({
token: signed_token,
Expand Down Expand Up @@ -757,6 +769,8 @@ export function authRoutes(fastify: FastifyInstance) {
external_user: user!.external_user,
};

await tracking("user_profile", {});

reply.send({
user: data,
});
Expand Down Expand Up @@ -1008,6 +1022,8 @@ export function authRoutes(fastify: FastifyInstance) {
},
});

await tracking("user_first_login", {});

reply.send({ success: true });
}
}
Expand Down
28 changes: 18 additions & 10 deletions apps/api/src/controllers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ const nodemailer = require("nodemailer");

import { checkToken } from "../lib/jwt";
import { prisma } from "../prisma";
import { emit } from "process";
import { createTransportProvider } from "../lib/nodemailer/transport";
import { track } from "../lib/hog";

async function tracking(event: string, properties: any) {
const client = track();

client.capture({
event: event,
properties: properties,
distinctId: "uuid",
});
}

export function configRoutes(fastify: FastifyInstance) {
// Check auth method
Expand Down Expand Up @@ -87,6 +97,8 @@ export function configRoutes(fastify: FastifyInstance) {
});
}

await tracking("oidc_provider_updated", {});

reply.send({
success: true,
message: "OIDC config Provider updated!",
Expand Down Expand Up @@ -152,6 +164,8 @@ export function configRoutes(fastify: FastifyInstance) {
});
}

await tracking("oauth_provider_updated", {});

reply.send({
success: true,
message: "SSO Provider updated!",
Expand Down Expand Up @@ -183,6 +197,8 @@ export function configRoutes(fastify: FastifyInstance) {
// Delete the OAuth provider
await prisma.oAuthProvider.deleteMany({});

await tracking("sso_provider_deleted", {});

reply.send({
success: true,
message: "SSO Provider deleted!",
Expand Down Expand Up @@ -211,14 +227,7 @@ export function configRoutes(fastify: FastifyInstance) {
},
});

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

if (config?.active) {
if (config && config?.active) {
const provider = await createTransportProvider();

await new Promise((resolve, reject) => {
Expand Down Expand Up @@ -247,7 +256,6 @@ export function configRoutes(fastify: FastifyInstance) {
reply.send({
success: true,
active: false,
email: config,
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/controllers/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function dataRoutes(fastify: FastifyInstance) {

if (token) {
const result = await prisma.ticket.count({
where: { userId: null, hidden: false },
where: { userId: null, hidden: false, isComplete: false },
});

reply.send({ count: result });
Expand Down
15 changes: 15 additions & 0 deletions apps/api/src/controllers/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { checkToken } from "../lib/jwt";
import { checkSession } from "../lib/session";
import { prisma } from "../prisma";
import { track } from "../lib/hog";

async function tracking(event: string, properties: any) {
const client = track();

client.capture({
event: event,
properties: properties,
distinctId: "uuid",
});

client.shutdownAsync();
}

export function notebookRoutes(fastify: FastifyInstance) {
// Create a new entry
Expand All @@ -25,6 +38,8 @@ export function notebookRoutes(fastify: FastifyInstance) {
},
});

await tracking("note_created", {});

const { id } = data;

reply.status(200).send({ success: true, id });
Expand Down
72 changes: 53 additions & 19 deletions apps/api/src/controllers/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { checkToken } from "../lib/jwt";
import { prisma } from "../prisma";
import { OAuth2Client } from "google-auth-library";
import { track } from "../lib/hog";

async function tracking(event: string, properties: any) {
const client = track();

client.capture({
event: event,
properties: properties,
distinctId: "uuid",
});

client.shutdownAsync();
}

export function emailQueueRoutes(fastify: FastifyInstance) {
// Create a new email queue
Expand Down Expand Up @@ -41,26 +54,47 @@ export function emailQueueRoutes(fastify: FastifyInstance) {
});

// generate redirect uri
if (serviceType === "gmail") {
const google = new OAuth2Client(clientId, clientSecret, redirectUri);

const authorizeUrl = google.generateAuthUrl({
access_type: "offline",
scope: "https://mail.google.com",
prompt: "consent",
state: mailbox.id,
});

reply.send({
success: true,
message: "Gmail imap provider created!",
authorizeUrl: authorizeUrl,
});
switch (serviceType) {
case "gmail":
const google = new OAuth2Client(
clientId,
clientSecret,
redirectUri
);

const authorizeUrl = google.generateAuthUrl({
access_type: "offline",
scope: "https://mail.google.com",
prompt: "consent",
state: mailbox.id,
});

tracking("gmail_provider_created", {
provider: "gmail",
});

reply.send({
success: true,
message: "Gmail imap provider created!",
authorizeUrl: authorizeUrl,
});
break;
case "other":
tracking("imap_provider_created", {
provider: "other",
});

reply.send({
success: true,
message: "Other service type created!",
});
break;
default:
reply.send({
success: false,
message: "Unsupported service type",
});
}

reply.send({
success: true,
});
}
}
);
Expand Down
29 changes: 19 additions & 10 deletions apps/api/src/controllers/ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,20 +443,29 @@ export function ticketRoutes(fastify: FastifyInstance) {
const { user, id }: any = request.body;

if (token) {
const assigned = await prisma.user.update({
where: { id: user },
data: {
tickets: {
connect: {
id: id,
if (user) {
const assigned = await prisma.user.update({
where: { id: user },
data: {
tickets: {
connect: {
id: id,
},
},
},
},
});
});

const { email } = assigned;
const { email } = assigned;

await sendAssignedEmail(email);
await sendAssignedEmail(email);
} else {
await prisma.ticket.update({
where: { id: id },
data: {
userId: null,
},
});
}

reply.send({
success: true,
Expand Down
Loading

0 comments on commit 5605536

Please sign in to comment.