Skip to content

Commit

Permalink
Format code with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
j.krol committed Nov 25, 2023
1 parent cd83426 commit 0a279af
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 141 deletions.
50 changes: 25 additions & 25 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ jobs:
contents: write

steps:
- uses: actions/checkout@v3
with:
- uses: actions/checkout@v3
with:
ref: ${{ github.ref }}

- uses: isbang/[email protected]
with:
compose-file: "compose.yml"
- name: Wait for containers to start
run: |
sleep 18
docker run \
--rm \
--network mercury-project_default \
alpine/curl -o /dev/null --retry 3 --retry-connrefused backend:5000
- name: Test the backend
run: cd backend; npm i && npm run test run

- name: Format files
run: cd ..; npm i && npm run prettier:fix

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Apply formatting changes
branch: ${{ github.head_ref }}
- uses: isbang/[email protected]
with:
compose-file: "compose.yml"

- name: Wait for containers to start
run: |
sleep 18
docker run \
--rm \
--network mercury-project_default \
alpine/curl -o /dev/null --retry 3 --retry-connrefused backend:5000
- name: Test the backend
run: cd backend; npm i && npm run test run

- name: Format files
run: cd ..; npm i && npm run prettier:fix

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Apply formatting changes
branch: ${{ github.head_ref }}
2 changes: 1 addition & 1 deletion backend/src/data/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ const userData = [
},
];

export default userData;
export default userData;
6 changes: 3 additions & 3 deletions backend/src/dtos/createMeeting.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default interface CreateMeetingDto {
ownerId: string;
guestId: string;
}
ownerId: string;
guestId: string;
}
85 changes: 49 additions & 36 deletions backend/src/httpServer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sign, verify } from "jsonwebtoken";
import {v4} from "uuid";
import { v4 } from "uuid";
import dotenv from "dotenv";
import servers from "./server";
import driver from "./driver/driver";
Expand All @@ -8,7 +8,7 @@ import authRouter from "./routes/authRoute";
import importInitialData from "./data/importData";
import CreateMeetingDto from "./dtos/createMeeting";

const {app} = servers;
const { app } = servers;

dotenv.config();
const linkSecret = process.env.LINK_SECRET;
Expand All @@ -19,52 +19,65 @@ app.use("/users", usersRouter);
app.use("/auth", authRouter);

app.post("/meeting", async (req, res) => {
try {
const {ownerId, guestId} = req.body as CreateMeetingDto;
const meetingId = v4();
const session = driver.session();
const newMeetingRequest = await session.run(`
try {
const { ownerId, guestId } = req.body as CreateMeetingDto;
const meetingId = v4();
const session = driver.session();
const newMeetingRequest = await session.run(
`
MATCH (u1:User) WHERE u1.id=$ownerId
MATCH (u2:User) WHERE u2.id=$guestId
MATCH (u1)-[:IS_FRIENDS_WITH]-(u2)
WHERE NOT (u1)-[:MEETING]-(u2)
CREATE (u1)-[m:MEETING {meetingId: $meetingId}]->(u2)
RETURN m
`, {ownerId, guestId, meetingId});
await session.close();
if (newMeetingRequest.records.length === 0) {
return res.status(404).json({ status: "error", errors: {message: "Cannot create new meeting"} });
}
const token = sign({ownerId, guestId, meetingId}, linkSecret!);
return res.json({token});
} catch (err) {
console.log("Error:", err);
return res.status(404).json({ status: "error", errors: err as object });
`,
{ ownerId, guestId, meetingId },
);
await session.close();
if (newMeetingRequest.records.length === 0) {
return res
.status(404)
.json({
status: "error",
errors: { message: "Cannot create new meeting" },
});
}
const token = sign({ ownerId, guestId, meetingId }, linkSecret!);
return res.json({ token });
} catch (err) {
console.log("Error:", err);
return res.status(404).json({ status: "error", errors: err as object });
}
});

app.post("/decode", (req, res) => {
const {token} = req.body as {token: string};
const decodedData = verify(token, linkSecret!);
return res.json({decodedData});
const { token } = req.body as { token: string };
const decodedData = verify(token, linkSecret!);
return res.json({ decodedData });
});

app.get("/guest-token/:guestId", async (req, res) => {
try {
const guestId = req.params.guestId;
const session = driver.session();
const guestRequest = await session.run(`
try {
const guestId = req.params.guestId;
const session = driver.session();
const guestRequest = await session.run(
`
MATCH (u:User) WHERE u.id=$guestId RETURN u
`,{guestId});
await session.close();
if (guestRequest.records.length === 0) {
return res.status(404).json({ status: "error", errors: {message: "User does not exist"} });
}
const guest = guestRequest.records[0].get(0).properties;
const token = sign(guest, linkSecret!);
return res.json({token});
} catch (err) {
console.log("Error:", err);
return res.status(404).json({ status: "error", errors: err as object });
`,
{ guestId },
);
await session.close();
if (guestRequest.records.length === 0) {
return res
.status(404)
.json({ status: "error", errors: { message: "User does not exist" } });
}
});
const guest = guestRequest.records[0].get(0).properties;
const token = sign(guest, linkSecret!);
return res.json({ token });
} catch (err) {
console.log("Error:", err);
return res.status(404).json({ status: "error", errors: err as object });
}
});
2 changes: 1 addition & 1 deletion backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import "./httpServer";
import "./socketServer";
import "./socketServer";
7 changes: 5 additions & 2 deletions backend/src/misc/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ export async function authenticateToken(
}
}

export async function decodeSocketData(handshakeData: string, linkSecret:string) {
export async function decodeSocketData(
handshakeData: string,
linkSecret: string,
) {
try {
const decodedData = jwt.verify(handshakeData, linkSecret);
return decodedData;
} catch (_e) {
return null;
}
}
}
2 changes: 1 addition & 1 deletion backend/src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export default interface User {
password: string;
friend_ids?: number[];
chats?: Chat[];
}
}
22 changes: 13 additions & 9 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import express, {Express} from "express";
import express, { Express } from "express";
import dotenv from "dotenv";
import cors from "cors";
import { Server as SocketServer } from "socket.io";
Expand All @@ -8,25 +8,29 @@ import cookieParser from "cookie-parser";
dotenv.config();

const corsOptions = {
origin: ["http://localhost:5000", "http://localhost:5173"],
optionsSuccessStatus: 200,
origin: ["http://localhost:5000", "http://localhost:5173"],
optionsSuccessStatus: 200,
};

const app: Express = express();
const port: number = 5000;

app.use(cors(corsOptions));
app.use((_req, res, next) => {
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE");
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
});
app.use(cookieParser());
app.use(express.json());

const expressServer = createServer(app);
const io = new SocketServer(expressServer, {cors: {origin: ["http://localhost:5173"]}});
const io = new SocketServer(expressServer, {
cors: { origin: ["http://localhost:5173"] },
});

expressServer.listen(port, () => console.log(`HTTP server running on port ${port}`));
expressServer.listen(port, () =>
console.log(`HTTP server running on port ${port}`),
);

export default {expressServer, io, app}
export default { expressServer, io, app };
19 changes: 9 additions & 10 deletions backend/src/socketServer.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import servers from "./server";
import dotenv from "dotenv";
import { Socket } from "socket.io";
import {decodeSocketData} from "./misc/jwt";
const {io, app} = servers;
import { decodeSocketData } from "./misc/jwt";
const { io, app } = servers;

dotenv.config();
const linkSecret = process.env.LINK_SECRET;

io.on("connection", (socket: Socket) => {
const handshakeData = socket.handshake.auth.jwt;
const decodedData = decodeSocketData(handshakeData, linkSecret!);
if (!decodedData) {
socket.disconnect();
return;
}

});
const handshakeData = socket.handshake.auth.jwt;
const decodedData = decodeSocketData(handshakeData, linkSecret!);
if (!decodedData) {
socket.disconnect();
return;
}
});
2 changes: 1 addition & 1 deletion compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ services:
build: ./frontend
ports:
- 5173:80

backend:
build: ./backend
env_file:
Expand Down
14 changes: 10 additions & 4 deletions frontend/src/components/ProfilePageForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,16 @@ function ProfilePageForm(props: ProfilePageFormProps) {
</p>
<ul>
Friends:{" "}
{props.friends.map(f => <li key={f.id}>
<h3>{f.first_name} {f.last_name}</h3>
<button onClick={() => console.log("meeting")}>launch meeting</button>
</li>)}
{props.friends.map((f) => (
<li key={f.id}>
<h3>
{f.first_name} {f.last_name}
</h3>
<button onClick={() => console.log("meeting")}>
launch meeting
</button>
</li>
))}
</ul>
</div>
<div className="my-5">
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/pages/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ function ProfilePage() {

useEffect(() => {
const fetchFriends = async () => {
const friendsResponse = await dataService.fetchData(`/users/${userId}/friends`,"GET",{});
const friendsResponse = await dataService.fetchData(
`/users/${userId}/friends`,
"GET",
{},
);
setFriends(friendsResponse.friends);
}
};
fetchFriends();
}, []);

Expand Down
20 changes: 9 additions & 11 deletions frontend/src/services/data.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
class DataService {

private url: string = "http://localhost:5000";

async fetchData(endpoint: string, method: string, options = {}) {
try {
const response = await fetch(this.url + endpoint, { ...options, method });
const data = await response.json();
return data;
} catch (error) {
console.error("Error ocurred during fetch data:", error);
throw error;
}
try {
const response = await fetch(this.url + endpoint, { ...options, method });
const data = await response.json();
return data;
} catch (error) {
console.error("Error ocurred during fetch data:", error);
throw error;
}
}

}

export default new DataService();
Loading

0 comments on commit 0a279af

Please sign in to comment.