Skip to content

Commit

Permalink
added railway deployment github action
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-ahnaf committed Sep 5, 2024
1 parent 338c234 commit 6392783
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 57 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/develop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Deploy to Railway

on:
push:
branches:
- develop
paths-ignore:
- 'README.md'

env:
AZURE_WEBAPP_NAME: bookify
NODE_VERSION: '20.x'

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Source
uses: actions/checkout@v3

- name: Setup Node.js version
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'

- name: Create .env file
run: |
echo "SQLITE_DB=${{ secrets.SQLITE_DB }}" >> .env
echo "TYPEORM_CLI=${{ secrets.TYPEORM_CLI }}" >> .env
echo "APP_PORT=${{ secrets.APP_PORT }}" >> .env
echo "NODE_ENV=${{ secrets.NODE_ENV }}" >> .env
echo "OAUTH_CLIENT_SECRET=${{ secrets.OAUTH_CLIENT_SECRET }}" >> .env
echo "OAUTH_CLIENT_ID=${{ secrets.OAUTH_CLIENT_ID }}" >> .env
echo "OAUTH_REDIRECT_URL=${{ secrets.OAUTH_REDIRECT_URL }}" >> .env
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> .env
echo "${{ secrets.ROOMS }}" > ./src/config/rooms.ts
- name: Install Dependencies
run: npm install

- name: Run database migrations
run: npm run migration:run

- name: Build project
run: npm run build

- name: Upload artifact for deployment job
uses: actions/upload-artifact@v4
with:
name: node-app
path: .

deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

steps:
- name: Download artifact from build job
uses: actions/download-artifact@v4
with:
name: node-app

- name: Install Railway
run: npm i -g @railway/cli

- name: Deploy
run: railway up --service bookify
env:
RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
7 changes: 4 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ jobs:
echo "${{ secrets.ROOMS }}" > ./src/config/rooms.ts
- name: Install Dependencies
run: |
npm install
npm run build --if-present
run: npm install

- name: Run database migrations
run: npm run migration:run

- name: Build project
run: npm run build

- name: Upload artifact for deployment job
uses: actions/upload-artifact@v4
with:
Expand Down
40 changes: 36 additions & 4 deletions client/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let floor = 1;

const CLIENT_ID = '1043931677993-j15eelb1golb8544ehi2meeru35q3fo4.apps.googleusercontent.com';
const REDIRECT_URI = window.location.origin;
const BACKEND_ENDPOINT = 'http://localhost:3000';
const BACKEND_ENDPOINT = REDIRECT_URI;

async function openPage(pageName, elmnt) {
var i, tabcontent, tablinks;
Expand Down Expand Up @@ -119,12 +119,17 @@ async function bookRoom() {
const startTimeSelect = document.getElementById('startTime');
const startTime = startTimeSelect.value;

const date = new Date(Date.now()).toISOString().split('T')[0];
const formattedStartTime = convertToRFC3339(date, startTime);

console.log('formattedStartTime', formattedStartTime);

const duration = document.getElementById('duration').textContent;
const seats = document.getElementById('seat_text').textContent;
const floor = document.getElementById('floor_text').textContent;

const res = await makeRequest('/room', 'POST', {
startTime,
startTime: formattedStartTime,
duration: parseInt(duration),
seats: parseInt(seats),
floor: parseInt(floor),
Expand All @@ -135,7 +140,7 @@ async function bookRoom() {
return;
}

createRoomAlert(res.room, res.start, res.end, res.summary, 'info');
createRoomAlert(res.room, convertToLocaleTime(res.start), convertToLocaleTime(res.end), res.summary, 'info');
}

async function makeRequest(path, method, body) {
Expand Down Expand Up @@ -340,7 +345,7 @@ const populateEvents = async () => {
<div class="row">
<div class="col-7 p-0" style="text-align: center;">
<div class="my_events_field" style="background-color: #ffec99; padding-right: 10px">
<span id="event_time" class="event_name">${event.start} - ${event.end}</span>
<span id="event_time" class="event_name">${convertToLocaleTime(event.start)} - ${convertToLocaleTime(event.end)}</span>
</div>
</div>
<div class="col-5 p-0">
Expand Down Expand Up @@ -381,3 +386,30 @@ async function removeEvent(id) {
eventElement.remove();
}
}

function convertToRFC3339(dateString, timeString, timeZoneOffset = '+06:00') {
const date = new Date(`${dateString} ${timeString}`);

const [offsetSign, offsetHours, offsetMinutes] = timeZoneOffset.match(/([+-])(\d{2}):(\d{2})/).slice(1);

const offsetInMinutes = (parseInt(offsetHours) * 60 + parseInt(offsetMinutes)) * (offsetSign === '+' ? 1 : -1);
date.setMinutes(date.getMinutes() + offsetInMinutes);

const isoString = date.toISOString();
const [isoDate, isoTime] = isoString.split('T');

// Return the formatted date and time in RFC 3339 format
return `${isoDate}T${isoTime.split('.')[0]}${timeZoneOffset}`;
}

function convertToLocaleTime(dateStr) {
const date = new Date(dateStr);

const options = {
hour: '2-digit',
minute: '2-digit',
hour12: true,
};

return date.toLocaleTimeString('en-US', options);
}
9 changes: 2 additions & 7 deletions src/calender/calender.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { CalenderService } from './calender.service';
import { AuthGuard } from '../auth/auth.guard';
import { _OAuth2Client } from '../auth/decorators';
import { EventResponse, RoomResponse } from './dto';
import { convertToRFC3339 } from './util/calender.util';
import { DeleteResponse } from './dto/delete.response';

@Controller()
Expand All @@ -29,16 +28,12 @@ export class CalenderController {
@Body('floor') floor?: number,
@Body('attendees') attendees?: string[],
): Promise<EventResponse | null> {
// start time
const date = new Date(Date.now()).toISOString().split('T')[0];
const formattedStartTime = convertToRFC3339(date, startTime);

// end time
const startDate = new Date(formattedStartTime);
const startDate = new Date(startTime);
startDate.setMinutes(startDate.getMinutes() + durationInMins);
const endTime = startDate.toISOString();

const event = await this.calenderService.createEvent(client, formattedStartTime, endTime, seats, createConference, title, floor, attendees);
const event = await this.calenderService.createEvent(client, startTime, endTime, seats, createConference, title, floor, attendees);
return event;
}

Expand Down
23 changes: 14 additions & 9 deletions src/calender/calender.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { OAuth2Client } from 'google-auth-library';
import { ConflictException, ForbiddenException, Inject, Injectable, InternalServerErrorException, NotImplementedException } from '@nestjs/common';
import { ConfigType } from '@nestjs/config';
import { calendar_v3, google } from 'googleapis';
import { google } from 'googleapis';
import appConfig from '../config/env/app.config';
import { EventResponse, RoomResponse } from './dto';
import { convertToLocaleTime, isRoomAvailable, parseLocation } from './util/calender.util';
import { isRoomAvailable, parseLocation } from './util/calender.util';
import { Room } from './interfaces/room.interface';
import { AuthService } from '../auth/auth.service';
import { rooms } from '../config/rooms';
Expand All @@ -27,6 +27,11 @@ export class CalenderService {
floor?: number,
attendees?: string[],
): Promise<EventResponse | null> {
console.log('current server date', new Date().toISOString());

console.log('startTime', startTime);
console.log('endTime', endTime);

const room = await this.getAvailableRoom(client, startTime, endTime, seats, floor);

if (!room) {
Expand Down Expand Up @@ -90,8 +95,8 @@ export class CalenderService {
return {
summary: result.data.summary,
meet: result.data.hangoutLink,
start: convertToLocaleTime(result.data.start.dateTime),
end: convertToLocaleTime(result.data.end.dateTime),
start: result.data.start.dateTime,
end: result.data.end.dateTime,
room: formattedRoom,
};
}
Expand All @@ -101,7 +106,7 @@ export class CalenderService {
const calendar = google.calendar({ version: 'v3', auth: client });
const filteredRoomIds = [];
for (const room of rooms) {
if (room.seats >= minSeats && (!floor || room.floor === floor)) {
if (room.seats >= minSeats && room.floor === floor) {
filteredRoomIds.push(room.id);
}
}
Expand All @@ -111,9 +116,9 @@ export class CalenderService {
timeMin: start,
timeMax: end,
timeZone: 'Asia/Dhaka',
items: rooms.map((room) => {
items: filteredRoomIds.map((id) => {
return {
id: room.id,
id
};
}),
},
Expand Down Expand Up @@ -156,8 +161,8 @@ export class CalenderService {
room: parseLocation(event.location),
id: event.id,
title: event.summary,
start: convertToLocaleTime(event.start.dateTime),
end: convertToLocaleTime(event.end.dateTime),
start: event.start.dateTime,
end: event.end.dateTime,
} as RoomResponse;
});

Expand Down
34 changes: 0 additions & 34 deletions src/calender/util/calender.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,3 @@ export function parseLocation(location: string) {
const parsedLocation = location.split(',');
return parsedLocation.length > 0 ? parsedLocation[0] : undefined;
}

/**
*
* @param dateString in the format '2024-08-31'
* @param timeString in the format '11:30 AM'
* @param timeZoneOffset
* @returns
*/
export function convertToRFC3339(dateString: string, timeString: string, timeZoneOffset = '+06:00') {
const date = new Date(`${dateString} ${timeString}`);

const [offsetSign, offsetHours, offsetMinutes] = timeZoneOffset.match(/([+-])(\d{2}):(\d{2})/).slice(1);

const offsetInMinutes = (parseInt(offsetHours) * 60 + parseInt(offsetMinutes)) * (offsetSign === '+' ? 1 : -1);
date.setMinutes(date.getMinutes() + offsetInMinutes);

const isoString = date.toISOString();
const [isoDate, isoTime] = isoString.split('T');

// Return the formatted date and time in RFC 3339 format
return `${isoDate}T${isoTime.split('.')[0]}${timeZoneOffset}`;
}

export function convertToLocaleTime(dateStr: string) {
const date = new Date(dateStr);

const options: Intl.DateTimeFormatOptions = {
hour: '2-digit',
minute: '2-digit',
hour12: true,
};

return date.toLocaleTimeString('en-US', options);
}

0 comments on commit 6392783

Please sign in to comment.