Skip to content
Open
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
11,094 changes: 37 additions & 11,057 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "ttsc -p tsconfig.build.json",
"prepare": "husky install",
"start": "node dist/server.js",
"migration:run": "prisma migrate deploy",
"migration:run": "npm run dev:load-envs prisma migrate deploy",
"migration:generate": "npm run dev:load-envs prisma migrate dev",
"lint:staged": "lint-staged",
"test": "jest --passWithNoTests --runInBand",
Expand All @@ -34,7 +34,8 @@
"dev:docker:down": "npm run dev:compose -- down",
"dev": "NODE_ENV=development nodemon --watch 'src/' --exec 'ts-node -r tsconfig-paths/register ./src/server.ts' -e ts",
"ci:build": "docker-compose -f docker-compose.test.yml build",
"ci:test": "docker-compose -f docker-compose.test.yml run drivent-test bash -c 'npm run test:migration:run && npm test'"
"ci:test": "docker-compose -f docker-compose.test.yml run drivent-test bash -c 'npm run test:migration:run && npm test'",
"insert": "npm run dev:seed npx ts-node prisma/seed.ts"
},
"keywords": [
"driven.t",
Expand Down Expand Up @@ -76,7 +77,7 @@
"prisma": "^3.14.0",
"supertest": "^6.2.3",
"ts-jest": "^26.5.5",
"ts-node": "^10.7.0",
"ts-node": "^10.8.1",
"tsconfig-paths": "3.9.0",
"ttypescript": "^1.5.13",
"typescript": "4.2.4",
Expand Down
61 changes: 61 additions & 0 deletions prisma/migrations/20220609213517_hotel_model/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
-- CreateTable
CREATE TABLE "hotel" (
"id" SERIAL NOT NULL,
"name" VARCHAR(255) NOT NULL,
"imageUrl" VARCHAR(255) NOT NULL,

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

-- CreateTable
CREATE TABLE "accomodationsType" (
"id" SERIAL NOT NULL,
"type" VARCHAR(255) NOT NULL,

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

-- CreateTable
CREATE TABLE "accomodationsHotel" (
"id" SERIAL NOT NULL,
"hotelId" INTEGER NOT NULL,
"accomodationsTypeId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "room" (
"id" SERIAL NOT NULL,
"accomodationsTypeId" INTEGER NOT NULL,
"hotelId" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "bed" (
"id" SERIAL NOT NULL,
"roomId" INTEGER NOT NULL,
"enrollmentId" INTEGER,

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

-- AddForeignKey
ALTER TABLE "accomodationsHotel" ADD CONSTRAINT "accomodationsHotel_hotelId_fkey" FOREIGN KEY ("hotelId") REFERENCES "hotel"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "accomodationsHotel" ADD CONSTRAINT "accomodationsHotel_accomodationsTypeId_fkey" FOREIGN KEY ("accomodationsTypeId") REFERENCES "accomodationsType"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "room" ADD CONSTRAINT "room_hotelId_fkey" FOREIGN KEY ("hotelId") REFERENCES "hotel"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "room" ADD CONSTRAINT "room_accomodationsTypeId_fkey" FOREIGN KEY ("accomodationsTypeId") REFERENCES "accomodationsType"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "bed" ADD CONSTRAINT "bed_enrollmentId_fkey" FOREIGN KEY ("enrollmentId") REFERENCES "Enrollment"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "bed" ADD CONSTRAINT "bed_roomId_fkey" FOREIGN KEY ("roomId") REFERENCES "room"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:

- A unique constraint covering the columns `[type]` on the table `accomodationsType` will be added. If there are existing duplicate values, this will fail.

*/
-- AlterTable
ALTER TABLE "accomodationsType" ALTER COLUMN "type" SET DATA TYPE TEXT;

-- CreateIndex
CREATE UNIQUE INDEX "accomodationsType_type_key" ON "accomodationsType"("type");
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:

- A unique constraint covering the columns `[name]` on the table `hotel` will be added. If there are existing duplicate values, this will fail.

*/
-- CreateIndex
CREATE UNIQUE INDEX "hotel_name_key" ON "hotel"("name");
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:

- The primary key for the `accomodationsHotel` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to drop the column `id` on the `accomodationsHotel` table. All the data in the column will be lost.

*/
-- AlterTable
ALTER TABLE "accomodationsHotel" DROP CONSTRAINT "accomodationsHotel_pkey",
DROP COLUMN "id",
ADD CONSTRAINT "accomodationsHotel_pkey" PRIMARY KEY ("hotelId", "accomodationsTypeId");
42 changes: 42 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ model Enrollment {
Address Address[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
bed bed[]
}

model Address {
Expand All @@ -67,3 +68,44 @@ model Address {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model hotel {
id Int @id @default(autoincrement())
name String @unique @db.VarChar(255)
imageUrl String @db.VarChar(255)
accomodationsHotel accomodationsHotel[]
room room[]
}

model accomodationsType {
id Int @id @default(autoincrement())
type String @unique
accomodationsHotel accomodationsHotel[]
room room[]
}

model accomodationsHotel {
hotelId Int
hotel hotel @relation(fields: [hotelId], references: [id])
accomodationsTypeId Int
accomodationsType accomodationsType @relation(fields: [accomodationsTypeId], references: [id])

@@id([hotelId, accomodationsTypeId])
}

model room {
id Int @id @default(autoincrement())
accomodationsTypeId Int
accomodationsType accomodationsType @relation(fields: [accomodationsTypeId], references: [id])
hotelId Int
hotel hotel @relation(fields: [hotelId], references: [id])
bed bed[]
}

model bed {
id Int @id @default(autoincrement())
roomId Int
room room @relation(fields: [roomId], references: [id])
Enrollment Enrollment? @relation(fields: [enrollmentId], references: [id])
enrollmentId Int?
}
143 changes: 143 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ import { PrismaClient } from '@prisma/client';
import dayjs from 'dayjs';
const prisma = new PrismaClient();

const mockedHotels1 = {
name: 'Driven Resort',
imageUrl: 'https://i.imgur.com/TkKjHEN.png',
};

const mockedHotels2 = {
name: 'Driven Palace',
imageUrl: 'https://i.imgur.com/OL1V1ud.png',
};

const mockedHotels3 = {
name: 'Driven World',
imageUrl: 'https://i.imgur.com/0EmyUAW.png',
};

async function main() {
let event = await prisma.event.findFirst();
if (!event) {
Expand All @@ -16,6 +31,134 @@ async function main() {
});
}

await prisma.hotel.upsert({
where: {
name: mockedHotels1.name,
},
update: mockedHotels1,
create: mockedHotels1,
});

await prisma.hotel.upsert({
where: {
name: mockedHotels2.name,
},
update: mockedHotels2,
create: mockedHotels2,
});

await prisma.hotel.upsert({
where: {
name: mockedHotels3.name,
},
update: mockedHotels3,
create: mockedHotels3,
});

await prisma.accomodationsType.upsert({
where: {
type: 'single',
},
update: { type: 'single' },
create: { type: 'single' },
});

await prisma.accomodationsType.upsert({
where: {
type: 'double',
},
update: { type: 'double' },
create: { type: 'double' },
});

await prisma.accomodationsType.upsert({
where: {
type: 'triple',
},
update: { type: 'triple' },
create: { type: 'triple' },
});

const single = await prisma.accomodationsType.findUnique({ where: { type: 'single' } });
const double = await prisma.accomodationsType.findUnique({ where: { type: 'double' } });
const triple = await prisma.accomodationsType.findUnique({ where: { type: 'triple' } });

const hotel1 = await prisma.hotel.findUnique({ where: { name: mockedHotels1.name } });
const hotel2 = await prisma.hotel.findUnique({ where: { name: mockedHotels2.name } });
const hotel3 = await prisma.hotel.findUnique({ where: { name: mockedHotels3.name } });

await prisma.accomodationsHotel.createMany({
data: [
{ hotelId: hotel1.id, accomodationsTypeId: single.id },
{ hotelId: hotel1.id, accomodationsTypeId: double.id },
{ hotelId: hotel2.id, accomodationsTypeId: single.id },
{ hotelId: hotel2.id, accomodationsTypeId: double.id },
{ hotelId: hotel2.id, accomodationsTypeId: triple.id },
{ hotelId: hotel3.id, accomodationsTypeId: single.id },
{ hotelId: hotel3.id, accomodationsTypeId: double.id },
],
});

await prisma.room.createMany({
data: [
{ hotelId: hotel1.id, accomodationsTypeId: single.id },
{ hotelId: hotel1.id, accomodationsTypeId: single.id },
{ hotelId: hotel1.id, accomodationsTypeId: single.id },
{ hotelId: hotel1.id, accomodationsTypeId: single.id },
{ hotelId: hotel1.id, accomodationsTypeId: single.id },
{ hotelId: hotel1.id, accomodationsTypeId: double.id },
{ hotelId: hotel1.id, accomodationsTypeId: double.id },
{ hotelId: hotel1.id, accomodationsTypeId: double.id },
{ hotelId: hotel1.id, accomodationsTypeId: double.id },
{ hotelId: hotel1.id, accomodationsTypeId: double.id },
{ hotelId: hotel1.id, accomodationsTypeId: double.id },
{ hotelId: hotel2.id, accomodationsTypeId: single.id },
{ hotelId: hotel2.id, accomodationsTypeId: single.id },
{ hotelId: hotel2.id, accomodationsTypeId: single.id },
{ hotelId: hotel2.id, accomodationsTypeId: single.id },
{ hotelId: hotel2.id, accomodationsTypeId: double.id },
{ hotelId: hotel2.id, accomodationsTypeId: double.id },
{ hotelId: hotel2.id, accomodationsTypeId: double.id },
{ hotelId: hotel2.id, accomodationsTypeId: double.id },
{ hotelId: hotel2.id, accomodationsTypeId: triple.id },
{ hotelId: hotel2.id, accomodationsTypeId: triple.id },
{ hotelId: hotel2.id, accomodationsTypeId: triple.id },
{ hotelId: hotel2.id, accomodationsTypeId: triple.id },
{ hotelId: hotel2.id, accomodationsTypeId: triple.id },
{ hotelId: hotel3.id, accomodationsTypeId: single.id },
{ hotelId: hotel3.id, accomodationsTypeId: single.id },
{ hotelId: hotel3.id, accomodationsTypeId: double.id },
{ hotelId: hotel3.id, accomodationsTypeId: double.id },
],
});

const rooms = await prisma.room.findMany();

rooms.forEach(async (room) => {
switch (room.accomodationsTypeId) {
case single.id:
await prisma.bed.createMany({
data: [{ roomId: room.id }],
});
break;

case double.id:
await prisma.bed.createMany({
data: [{ roomId: room.id }, { roomId: room.id }],
});
break;

case triple.id:
await prisma.bed.createMany({
data: [{ roomId: room.id }, { roomId: room.id }, { roomId: room.id }],
});
break;

default:
break;
}
});

console.log({ event });
}

Expand Down
11 changes: 10 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import { loadEnv, connectDb, disconnectDB } from '@/config';
loadEnv();

import { handleApplicationErrors } from '@/middlewares';
import { usersRouter, authenticationRouter, eventsRouter, enrollmentsRouter } from '@/routers';
import {
usersRouter,
authenticationRouter,
eventsRouter,
enrollmentsRouter,
hotelsRouter,
roomsRouter,
} from '@/routers';

const app = express();
app
Expand All @@ -19,6 +26,8 @@ app
.use('/auth', authenticationRouter)
.use('/event', eventsRouter)
.use('/enrollments', enrollmentsRouter)
.use('/hotels', hotelsRouter)
.use('/room', roomsRouter)
.use(handleApplicationErrors);

export function init(): Promise<Express> {
Expand Down
9 changes: 9 additions & 0 deletions src/controllers/hotels-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import hotelsService from '@/services/hotels-service';
import { Request, Response } from 'express';
import httpStatus from 'http-status';

export async function getHotels(_req: Request, res: Response) {
const hotels = await hotelsService.getAllHotels();

return res.status(httpStatus.OK).send(hotels);
}
2 changes: 2 additions & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from './users-controller';
export * from './authentication-controller';
export * from './events-controller';
export * from './enrollments-controller';
export * from './hotels-controller';
export * from './rooms-controllers';
Loading