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

Next #182

Merged
merged 2 commits into from
Nov 25, 2023
Merged

Next #182

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
35 changes: 32 additions & 3 deletions apps/api/src/controllers/ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export function ticketRoutes(fastify: FastifyInstance) {

if (token) {
const tickets = await prisma.ticket.findMany({
where: { isComplete: false },
where: { isComplete: false, hidden: false },
include: {
client: {
select: { id: true, name: true, number: true },
Expand Down Expand Up @@ -187,7 +187,7 @@ export function ticketRoutes(fastify: FastifyInstance) {
const user = await checkSession(bearer);

const tickets = await prisma.ticket.findMany({
where: { isComplete: false, userId: user!.id },
where: { isComplete: false, userId: user!.id, hidden: false },
include: {
client: {
select: { id: true, name: true, number: true },
Expand Down Expand Up @@ -219,7 +219,7 @@ export function ticketRoutes(fastify: FastifyInstance) {

if (token) {
const tickets = await prisma.ticket.findMany({
where: { isComplete: true },
where: { isComplete: true, hidden: false },
include: {
client: {
select: { id: true, name: true, number: true },
Expand Down Expand Up @@ -254,6 +254,7 @@ export function ticketRoutes(fastify: FastifyInstance) {
where: {
isComplete: false,
assignedTo: null,
hidden: false,
},
});

Expand Down Expand Up @@ -430,6 +431,34 @@ export function ticketRoutes(fastify: FastifyInstance) {
});
}
}

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

// Hide a ticket
fastify.put(
"/api/v1/ticket/status/hide",

async (request: FastifyRequest, reply: FastifyReply) => {
const { hidden, id }: any = request.body;

await prisma.ticket
.update({
where: { id: id },
data: {
hidden: hidden,
},
})
.then(async (ticket) => {
// await sendTicketStatus(ticket);
});

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

Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ server.register(require("@fastify/swagger"), {
url: "https://swagger.io",
description: "Find more info here",
},
mode: "static",
host: "localhost",
schemes: ["http"],
consumes: ["application/json"],
Expand All @@ -37,6 +38,7 @@ server.register(require("@fastify/swagger"), {
{ name: "user", description: "User related end-points" },
{ name: "code", description: "Code related end-points" },
],
exposeRoute: true,
definitions: {
User: {
type: "object",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Ticket" ADD COLUMN "hidden" BOOLEAN NOT NULL DEFAULT false;
1 change: 1 addition & 0 deletions apps/api/src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ model Ticket {
Number Int @default(autoincrement())
status TicketStatus @default(needs_support)
type TicketType @default(support)
hidden Boolean @default(false)

TicketFile TicketFile[]
Comment Comment[]
Expand Down
5 changes: 2 additions & 3 deletions apps/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"docker": "yarn run migrate && yarn run generate && yarn run seed && yarn start",
"pm2-prod": "yarn run migrate && yarn run generate && yarn run seed && yarn run build && pm2 start npm --name \"next\" -- start"
"start": "next start"
},
"dependencies": {
"@headlessui/react": "^1.4.2",
Expand Down Expand Up @@ -47,6 +45,7 @@
"next-themes": "^0.0.15",
"next-translate": "^1.3.4",
"nodemailer": "^6.7.2",
"posthog-js": "^1.93.2",
"prosemirror-model": "^1.18.1",
"pug": "^3.0.2",
"react": "18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/client/pages/auth/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function Login({}) {
const { setUser } = useUser();

async function postData() {
await fetch("http://localhost:5003/api/v1/auth/login", {
await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/v1/auth/login`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, password }),
Expand Down
57 changes: 41 additions & 16 deletions apps/client/pages/tickets/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
ChevronUpDownIcon,
LockClosedIcon,
LockOpenIcon,
PencilIcon,
} from "@heroicons/react/20/solid";
import { Link, RichTextEditor } from "@mantine/tiptap";
import Highlight from "@tiptap/extension-highlight";
Expand All @@ -23,6 +22,7 @@ import renderHTML from "react-render-html";
import SubScript from "@tiptap/extension-subscript";
import Superscript from "@tiptap/extension-superscript";
import { getCookie } from "cookies-next";
import { useUser } from "../../store/session";

function classNames(...classes: any) {
return classes.filter(Boolean).join(" ");
Expand All @@ -33,6 +33,10 @@ export default function Ticket() {

const token = getCookie("session");

const { user } = useUser();

console.log(user);

const fetchTicketById = async () => {
const id = router.query.id;
const res = await fetch(
Expand Down Expand Up @@ -113,9 +117,9 @@ export default function Ticket() {

async function updateStatus() {
await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/ticket/update-status`,
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/ticket/status/update`,
{
method: "POST",
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
Expand All @@ -129,6 +133,26 @@ export default function Ticket() {
.then((res) => res.json())
.then(() => refetch());
}

async function hide(hidden) {
await fetch(
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/ticket/status/hide`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
hidden,
id,
}),
}
)
.then((res) => res.json())
.then(() => refetch());
}

async function addComment() {
await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/v1/ticket/comment`, {
method: "POST",
Expand Down Expand Up @@ -303,16 +327,23 @@ export default function Ticket() {
</p>
</div>
<div className="mt-4 flex space-x-3 md:mt-0">
{user.isAdmin && (
<button
type="button"
onClick={() => hide(!data.ticket.hidden)}
className="inline-flex justify-center items-center gap-x-1.5 rounded-md bg-white px-5 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
>
{data.ticket.hidden
? "Show Global"
: "Hide Ticket"}
</button>
)}
{!edit ? (
<button
type="button"
onClick={() => setEdit(true)}
className="inline-flex justify-center gap-x-1.5 rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
className="inline-flex justify-center items-center gap-x-1.5 rounded-md bg-white px-5 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
>
<PencilIcon
className="-ml-0.5 h-5 w-5 text-gray-400"
aria-hidden="true"
/>
Edit
</button>
) : (
Expand All @@ -322,12 +353,8 @@ export default function Ticket() {
update();
setEdit(false);
}}
className="inline-flex justify-center gap-x-1.5 rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
className="inline-flex justify-center gap-x-1.5 rounded-md bg-white px-5 py-2 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
>
<CheckIcon
className="-ml-0.5 h-5 w-5 text-gray-400"
aria-hidden="true"
/>
Save
</button>
)}
Expand Down Expand Up @@ -766,9 +793,7 @@ export default function Ticket() {
className="-ml-0.5 h-5 w-5 text-red-500"
aria-hidden="true"
/>
<span className="pt-1">
Re-Open issue
</span>
<span className="">Re-Open issue</span>
</button>
) : (
<button
Expand Down
2 changes: 0 additions & 2 deletions apps/client/prisma/prisma.js

This file was deleted.

44 changes: 24 additions & 20 deletions apps/client/store/session.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,64 @@
// UserContext.js
import { getCookie } from 'cookies-next';
import { useRouter } from 'next/router';
import { createContext, useContext, useEffect, useState } from 'react';
import { getCookie } from "cookies-next";
import { useRouter } from "next/router";
import posthog from "posthog-js";
import { PostHogProvider } from "posthog-js/react";
import { createContext, useContext, useEffect, useState } from "react";

const UserContext = createContext();

posthog.init(process.env.NEXT_PUBLIC_POSTHOG);

export const SessionProvider = ({ children }) => {
const router = useRouter();
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);

const fetchUserProfile = async () => {
const token = getCookie('session');
const token = getCookie("session");
try {
await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/v1/auth/profile`, {
method: 'GET',
method: "GET",
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
}).then(res => res.json())
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
})
.then((res) => res.json())
.then((res) => {
if(res.user){
setUser(res.user)
if (res.user) {
setUser(res.user);
setLoading(false);
} else {
console.error('Failed to fetch user profile');
router.push('/auth/login');
console.error("Failed to fetch user profile");
router.push("/auth/login");
}
})
});
} catch (error) {
// Handle fetch errors if necessary
console.error('Error fetching user profile:', error);
router.push('/auth/login');

console.error("Error fetching user profile:", error);
router.push("/auth/login");
} finally {
setLoading(false);
}
};

useEffect(() => {
console.log('fetching user profile');
console.log("fetching user profile");
fetchUserProfile();
}, []);

return (
<UserContext.Provider value={{ user, setUser, loading }}>
{children}
<PostHogProvider client={posthog}>{children}</PostHogProvider>
</UserContext.Provider>
);
};

export const useUser = () => {
const context = useContext(UserContext);
if (!context) {
throw new Error('useUser must be used within a UserProvider');
throw new Error("useUser must be used within a UserProvider");
}
return context;
};
2 changes: 1 addition & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
DB_USERNAME: "peppermint"
DB_PASSWORD: "1234"
DB_HOST: "peppermint_postgres"
API_PORT: 5003
API_URL: "http://localhost:5003"

volumes:
pgdata:
4 changes: 2 additions & 2 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
DB_USERNAME: "peppermint"
DB_PASSWORD: "1234"
DB_HOST: "peppermint_postgres"
API_PORT: 5003

API_URL: "http://localhost:5003"
volumes:
pgdata:
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ services:
DB_USERNAME: "peppermint"
DB_PASSWORD: "1234"
DB_HOST: "peppermint_postgres"
API_PORT: 5003
API_URL: "http://localhost:5003"

volumes:
pgdata:
7 changes: 4 additions & 3 deletions dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ RUN apt-get update && \
# Copy the package.json and package-lock.json files for both apps
COPY apps/api/package*.json ./apps/api/
COPY apps/client/package*.json ./apps/client/
COPY ./ecosystem.config.js ./ecosystem.config.js


RUN npm i -g prisma
RUN npm i -g typescript@latest -g --force
Expand All @@ -27,8 +29,7 @@ FROM node:lts AS runner

COPY --from=builder /app/apps/api/ ./apps/api/
COPY --from=builder /app/apps/client ./apps/client
COPY ./ecosystem.config.js ./ecosystem.config.js

COPY --from=builder /app/ecosystem.config.js ./ecosystem.config.js

# Expose the ports for both apps
EXPOSE 3000 5003
Expand All @@ -37,5 +38,5 @@ EXPOSE 3000 5003
RUN npm install -g pm2

# Start both apps using PM2
CMD ["pm2-runtime", "ecosystem.config .js"]
CMD ["pm2-runtime", "ecosystem.config.js"]

Loading
Loading