Skip to content

Commit

Permalink
Merge pull request #45 from sanam2405/loc
Browse files Browse the repository at this point in the history
synchronise Postgres and MongoDB
  • Loading branch information
sanam2405 authored Apr 28, 2024
2 parents 8dfe634 + 38c336c commit 9388c33
Show file tree
Hide file tree
Showing 27 changed files with 13,651 additions and 156 deletions.
19 changes: 19 additions & 0 deletions backend/loc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions backend/loc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@
"@types/bcryptjs": "^2.4.6",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/uuid": "^9.0.8",
"@types/wellknown": "^0.5.8",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"prettier": "^3.2.5",
"uuid": "^9.0.1",
"wellknown": "^0.5.0",
"zod": "^3.22.5"
}
Expand Down
27 changes: 0 additions & 27 deletions backend/loc/prisma/migrations/20240421103750_init/migration.sql

This file was deleted.

30 changes: 30 additions & 0 deletions backend/loc/prisma/migrations/20240428092122_dbseed/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- CreateTable
CREATE TABLE "Location" (
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
"latitude" DOUBLE PRECISION NOT NULL,
"longitude" DOUBLE PRECISION NOT NULL,
"createdAt" TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(6) NOT NULL,

CONSTRAINT "Location_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "User" (
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
"name" TEXT NOT NULL,
"email" TEXT NOT NULL,
"age" DOUBLE PRECISION NOT NULL,
"gender" TEXT NOT NULL,
"college" TEXT NOT NULL,
"isVisible" BOOLEAN NOT NULL,
"locationId" UUID,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_locationId_fkey" FOREIGN KEY ("locationId") REFERENCES "Location"("id") ON DELETE CASCADE ON UPDATE CASCADE;
18 changes: 9 additions & 9 deletions backend/loc/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ datasource db {
}

model Location {
id String @id @default(dbgenerated("gen_random_uuid()"))
latitude Float?
longitude Float?
createdAt DateTime @default(now())
updatedAt DateTime
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
latitude Float
longitude Float
createdAt DateTime @default(now()) @db.Timestamp(6)
updatedAt DateTime @db.Timestamp(6)
users User[]
}

model User {
id String @id @default(dbgenerated("gen_random_uuid()"))
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
name String
email String
email String @unique
age Float
gender String
college String
isVisible Boolean
locationId String?
location Location? @relation(fields: [locationId], references: [id])
locationId String? @db.Uuid
location Location? @relation(fields: [locationId], references: [id], onDelete: Cascade)
}
6 changes: 5 additions & 1 deletion backend/loc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import cors from "cors";
import globalCatch from "./middlewares/globalCatch";
import queryRouter from "./routes/query";
import pingRouter from "./routes/ping";
import authRouter from "./routes/auth";
import userRouter from "./routes/user";

const app = express();
const PORT: string | number = process.env.PORT || 6060;
Expand All @@ -16,8 +18,10 @@ app.get("/", (_req: Request, res: Response) => {
<pre> ~ Built with &#x1F499 by sanam </pre>`);
});

app.use("/api", queryRouter);
app.use("/api", pingRouter);
app.use("/api", authRouter);
app.use("/api", userRouter);
app.use("/api", queryRouter);

app.use(globalCatch);

Expand Down
48 changes: 48 additions & 0 deletions backend/loc/src/middlewares/interBackendAccess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Request, Response, NextFunction } from "express";
import HttpStatusCode from "../types/HttpStatusCode";
import bcrypt from "bcryptjs";

export const interBackendAccess = async (
req: Request,
res: Response,
next: NextFunction,
) => {
const { authorization } = req.headers;

try {
if (!authorization) {
return res
.status(HttpStatusCode.UNAUTHORIZED)
.send({ errors: "You are unauthorized to access this resource" });
}

if (!process.env.BACKEND_INTERCOMMUNICATION_SECRET) {
console.error(
"BACKEND_INTERCOMMUNICATION_SECRET is undefined. Check the .env",
);
return res
.status(HttpStatusCode.INTERNAL_SERVER_ERROR)
.send({ errors: "Internal Server Error" });
}

const token = authorization.replace("Bearer ", "");

const isMatch = bcrypt.compareSync(
process.env.BACKEND_INTERCOMMUNICATION_SECRET,
token,
);

if (!isMatch) {
return res
.status(HttpStatusCode.UNAUTHORIZED)
.send({ errors: "You are unauthorized to access this resource" });
}

next();
} catch (error) {
console.error(error);
return res
.status(HttpStatusCode.UNAUTHORIZED)
.send({ errors: "You must be logged in" });
}
};
Loading

1 comment on commit 9388c33

@vercel
Copy link

@vercel vercel bot commented on 9388c33 Apr 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.