Skip to content

Commit 11a67b1

Browse files
authored
Merge branch 'next' into fix/responsiveness-issue
2 parents ad31447 + b345f36 commit 11a67b1

File tree

24 files changed

+674
-494
lines changed

24 files changed

+674
-494
lines changed

.github/workflows/dev.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77

88
jobs:
99
build:
10-
runs-on: ubuntu-latest
10+
runs-on: self-hosted
1111
steps:
1212
- name: Get current time
1313
uses: 1466587594/get-current-time@v2
@@ -33,7 +33,7 @@ jobs:
3333
- name: Build and push
3434
uses: docker/build-push-action@v2
3535
with:
36-
platforms: linux/amd64,linux/arm64,linux/arm
36+
platforms: linux/amd64,linux/arm64
3737
push: true
3838
tags: |
39-
pepperlabs/peppermint:dev
39+
pepperlabs/peppermint:nightly

.github/workflows/main.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Main Client Build
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
build:
10+
runs-on: self-hosted
11+
steps:
12+
- name: Get current time
13+
uses: 1466587594/get-current-time@v2
14+
id: current-time
15+
with:
16+
format: YYYY-MM-DD--HH
17+
18+
- name: Checkout code
19+
uses: actions/checkout@v2
20+
21+
# Install QEMU-based emulator
22+
- name: Install QEMU
23+
run: apt-get update && apt-get install -y qemu-user-static
24+
25+
- name: Set up QEMU
26+
uses: docker/setup-qemu-action@v1
27+
28+
- name: Set up Docker Buildx
29+
uses: docker/setup-buildx-action@v2
30+
31+
- name: Login to DockerHub
32+
uses: docker/login-action@v1
33+
with:
34+
username: ${{ secrets.DOCKERHUB_USERNAME }}
35+
password: ${{ secrets.DOCKERHUB_PASSWORD }}
36+
37+
- name: Build and push
38+
uses: docker/build-push-action@v2
39+
with:
40+
platforms: linux/amd64,linux/arm64
41+
push: true
42+
tags: |
43+
pepperlabs/peppermint:latest

apps/api/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ dist
102102

103103
# TernJS port file
104104
.tern-port
105+
106+
/uploads

apps/api/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
},
1616
"devDependencies": {
1717
"@types/bcrypt": "^5.0.0",
18+
"@types/formidable": "^3.4.5",
1819
"@types/jsonwebtoken": "^8.5.8",
1920
"@types/node": "^17.0.23",
2021
"@types/nodemailer": "^6.4.14",
@@ -27,14 +28,19 @@
2728
"dependencies": {
2829
"@fastify/cookie": "^9.0.4",
2930
"@fastify/cors": "^8.3.0",
31+
"@fastify/multipart": "^8.2.0",
3032
"@fastify/rate-limit": "^9.0.0",
3133
"@fastify/session": "^10.4.0",
3234
"@fastify/swagger": "^8.10.0",
3335
"@prisma/client": "5.2.0",
36+
"add": "^2.0.6",
3437
"axios": "^1.5.0",
3538
"bcrypt": "^5.0.1",
3639
"dotenv": "^16.0.0",
3740
"fastify": "4.22.2",
41+
"fastify-formidable": "^3.0.2",
42+
"fastify-multer": "^2.0.3",
43+
"formidable": "^3.5.1",
3844
"got": "^13.0.0",
3945
"handlebars": "^4.7.8",
4046
"i": "^0.3.7",

apps/api/src/controllers/auth.ts

Lines changed: 96 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ export function authRoutes(fastify: FastifyInstance) {
373373
reply.send({
374374
oauth: true,
375375
success: true,
376-
ouath_url: `${url}?client_id=${oauth.clientId}&redirect_uri=${oauth.redirectUri}&state=${email}&login=${email}&scope=user`,
376+
ouath_url: `${url}?client_id=${oauth.clientId}&redirect_uri=${oauth.redirectUri}&login=${email}&scope=user`,
377377
});
378378
}
379379
);
@@ -384,62 +384,73 @@ export function authRoutes(fastify: FastifyInstance) {
384384
async (request: FastifyRequest, reply: FastifyReply) => {
385385
const { code, state } = request.query as { code: string; state: string };
386386

387-
const provider = await prisma.provider.findFirst({});
387+
console.log("HIT");
388388

389-
const data = await axios.post(
390-
`https://github.com/login/oauth/access_token`,
391-
{
392-
client_id: provider?.clientId,
393-
client_secret: provider?.clientSecret,
394-
code: code,
395-
redirect_uri: provider?.redirectUri,
396-
},
397-
{
398-
headers: {
399-
Accept: "application/json",
400-
},
401-
}
402-
);
389+
try {
390+
const provider = await prisma.provider.findFirst({});
391+
if (!provider) throw new Error("Provider not found");
403392

404-
const access_token = data.data;
393+
const { clientId, clientSecret, redirectUri } = provider;
405394

406-
if (access_token) {
407-
const gh = await axios.get(`https://api.github.com/user/emails`, {
408-
headers: {
409-
Accept: "application/vnd.github+json",
410-
Authorization: `token ${access_token.access_token}`,
395+
const { data: github_user } = await axios.post(
396+
`https://github.com/login/oauth/access_token`,
397+
{
398+
client_id: clientId,
399+
client_secret: clientSecret,
400+
code: code,
401+
redirect_uri: redirectUri,
411402
},
412-
});
403+
{
404+
headers: {
405+
Accept: "application/json",
406+
},
407+
}
408+
);
409+
410+
console.log(github_user);
411+
412+
// await new Promise((r) => setTimeout(r, 2000));
413+
414+
const { data: emails } = await axios.get(
415+
`https://api.github.com/user/emails`,
416+
{
417+
headers: {
418+
Accept: "application/vnd.github+json",
419+
Authorization: `Bearer ${github_user.access_token}`,
420+
},
421+
}
422+
);
413423

414-
const emails = gh.data;
424+
console.log(emails);
415425

416-
const filter = emails.filter((e: any) => e.primary === true);
426+
const primaryEmail = emails.find((e: any) => e.primary === true)?.email;
427+
if (!primaryEmail) throw new Error("Primary email not found");
417428

418429
let user = await prisma.user.findUnique({
419-
where: { email: filter[0].email },
430+
where: { email: primaryEmail },
420431
});
421432

422433
if (!user) {
423-
reply.send({
434+
return reply.send({
424435
success: false,
425436
message: "Invalid email",
426437
});
427438
}
428439

429440
var b64string = process.env.SECRET;
430-
var buf = new Buffer(b64string!, "base64"); // Ta-da
441+
var secret = new Buffer(b64string!, "base64"); // Ta-da
431442

432443
let token = jwt.sign(
433444
{
434-
data: { id: user!.id },
445+
data: { id: user.id },
435446
},
436-
buf,
447+
secret,
437448
{ expiresIn: "8h" }
438449
);
439450

440451
await prisma.session.create({
441452
data: {
442-
userId: user!.id,
453+
userId: user.id,
443454
sessionToken: token,
444455
expires: new Date(Date.now() + 8 * 60 * 60 * 1000),
445456
},
@@ -449,9 +460,11 @@ export function authRoutes(fastify: FastifyInstance) {
449460
token,
450461
success: true,
451462
});
452-
} else {
463+
} catch (error: any) {
464+
console.error("Authentication error:", error);
453465
reply.status(403).send({
454466
success: false,
467+
message: error.message || "Authentication failed",
455468
});
456469
}
457470
}
@@ -577,6 +590,57 @@ export function authRoutes(fastify: FastifyInstance) {
577590
}
578591
);
579592

593+
// Reset password by admin
594+
fastify.post(
595+
"/api/v1/auth/admin/reset-password",
596+
async (request: FastifyRequest, reply: FastifyReply) => {
597+
let { password, user } = request.body as {
598+
password: string;
599+
user: string;
600+
};
601+
602+
console.log(user);
603+
604+
const bearer = request.headers.authorization!.split(" ")[1];
605+
const token = checkToken(bearer);
606+
607+
if (token) {
608+
let session = await prisma.session.findUnique({
609+
where: {
610+
sessionToken: bearer,
611+
},
612+
});
613+
614+
const check = await prisma.user.findUnique({
615+
where: { id: session?.userId },
616+
});
617+
618+
if (check?.isAdmin === false) {
619+
reply.code(401).send({
620+
message: "Unauthorized",
621+
});
622+
}
623+
624+
const hashedPass = await bcrypt.hash(password, 10);
625+
626+
await prisma.user.update({
627+
where: { id: user },
628+
data: {
629+
password: hashedPass,
630+
},
631+
});
632+
633+
reply.send({
634+
success: true,
635+
});
636+
} else {
637+
reply.send({
638+
success: false,
639+
});
640+
}
641+
}
642+
);
643+
580644
// Update a users profile/config
581645
fastify.put(
582646
"/api/v1/auth/profile",
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//@ts-nocheck
2+
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
3+
import multer from "fastify-multer";
4+
import { prisma } from "../prisma";
5+
const upload = multer({ dest: "uploads/" });
6+
7+
export function objectStoreRoutes(fastify: FastifyInstance) {
8+
//
9+
fastify.post(
10+
"/api/v1/storage/ticket/:id/upload/single",
11+
{ preHandler: upload.single("file") },
12+
13+
async (request: FastifyRequest, reply: FastifyReply) => {
14+
console.log(request.file);
15+
console.log(request.body);
16+
17+
const uploadedFile = await prisma.ticketFile.create({
18+
data: {
19+
ticketId: request.params.id,
20+
filename: request.file.originalname,
21+
path: request.file.path,
22+
mime: request.file.mimetype,
23+
size: request.file.size,
24+
encoding: request.file.encoding,
25+
userId: request.body.user,
26+
},
27+
});
28+
29+
console.log(uploadedFile);
30+
31+
reply.send({
32+
success: true,
33+
});
34+
}
35+
);
36+
37+
// Get all ticket attachments
38+
39+
// Delete an attachment
40+
41+
// Download an attachment
42+
}

apps/api/src/controllers/ticket.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,17 @@ export function ticketRoutes(fastify: FastifyInstance) {
183183
},
184184
});
185185

186+
const files = await prisma.ticketFile.findMany({
187+
where: {
188+
ticketId: id,
189+
},
190+
});
191+
186192
var t = {
187193
...ticket,
188194
comments: [...comments],
189195
TimeTracking: [...timeTracking],
196+
files: [...files],
190197
};
191198

192199
reply.send({

apps/api/src/controllers/users.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,20 @@ export function userRoutes(fastify: FastifyInstance) {
1515
if (bearer) {
1616
const token = checkToken(bearer);
1717
if (token) {
18-
const users = await prisma.user.findMany({});
18+
const users = await prisma.user.findMany({
19+
where: {
20+
external_user: false,
21+
},
22+
select: {
23+
id: true,
24+
name: true,
25+
email: true,
26+
isAdmin: true,
27+
createdAt: true,
28+
updatedAt: true,
29+
language: true,
30+
},
31+
});
1932

2033
reply.send({
2134
users,

0 commit comments

Comments
 (0)