Skip to content

Commit

Permalink
Merge pull request #47 from cepdnaclk/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
KATTA-00 authored Jan 13, 2024
2 parents 4c83c08 + 04e4182 commit 2f5401b
Show file tree
Hide file tree
Showing 53 changed files with 1,390 additions and 863 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/self.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ jobs:
npm install
npm run build
cp .env ./dist
pm2 restart backendserver
chmod +x run.sh
./run.sh
pm2 save
22 changes: 0 additions & 22 deletions code/backend/.elasticbeanstalk/config.yml

This file was deleted.

23 changes: 23 additions & 0 deletions code/backend/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"configurations": [
{
"name": "AVR",
"intelliSenseMode": "${default}",
"compilerPath": "",
"cStandard": "${default}",
"cppStandard": "${default}",
"includePath": [],
"compilerArgs": [
"-g",
"-Os",
"-Wall",
"-Wextra",
"-fpermissive",
"-fno-exceptions",
"-fno-threadsafe-statics",
"-pipe"
]
}
],
"version": 4
}
File renamed without changes
2 changes: 2 additions & 0 deletions code/backend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testTimeout: 10000, // or any other value in milliseconds
maxWorkers: 4,

clearMocks: true,
coverageProvider: "v8",
Expand Down
2 changes: 1 addition & 1 deletion code/backend/package-lock.json

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

2 changes: 1 addition & 1 deletion code/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"typescript": "^5.3.2"
},
"engines": {
"node": "14"
"node": "18"
},
"resolutions": {
"fsevents": "^2.3.3"
Expand Down
10 changes: 10 additions & 0 deletions code/backend/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# Check if backendserver is running
if pm2 describe backendserver &> /dev/null; then
# If running, restart it
pm2 restart backendserver
else
# If not running, start a new instance
pm2 start ./dist/index.js --name backendserver
fi
38 changes: 22 additions & 16 deletions code/backend/src/controllers/hub.controller.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import { Hub, HubRequest, HubResponse } from "../models/hub.model";
import { getHubDetails, createHub } from "../services/hub.service";
import hubService from "../services/hub.service";

async function getHub(hubReq: HubRequest): Promise<HubResponse> {
try {
const hub: Hub = await getHubDetails(hubReq.hubId, hubReq.hubKey);
return new HubResponse(hub.mqttUsername, hub.mqttPassword, hub.mqttCA);
} catch (err) {
throw err;
class HubController {
async getHub(hubReq: HubRequest): Promise<HubResponse> {
try {
const hub: Hub = await hubService.getHubDetails(
hubReq.hubId,
hubReq.hubKey
);
return new HubResponse(hub.mqttUsername, hub.mqttPassword, hub.mqttCA);
} catch (err) {
throw err;
}
}
}

// create hub
async function createHubDetails(newhub: Hub): Promise<Hub> {
try {
const hub: Hub = await createHub(newhub);
return hub;
} catch (err) {
throw err;
// create hub
async createHubDetails(newhub: Hub): Promise<Hub> {
try {
const hub: Hub = await hubService.createHub(newhub);
return hub;
} catch (err) {
throw err;
}
}
}
export { getHub, createHubDetails };

export default new HubController();
156 changes: 77 additions & 79 deletions code/backend/src/controllers/login.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,117 +3,115 @@ import {
LoginResponse,
LoginResquestManager,
} from "../models/login.model";
import {
checkAuthExists,
checkAuth,
checkAuthExistsForManager,
checkAuthManager,
} from "../services/auth.service";
import { createJwt, checkJwtExists, deleteJwt } from "../services/jwt.service";
import authService from "../services/auth.service";
import jwtService from "../services/jwt.service";
import {
createRefreshToken,
createAccessToken,
createRefreshTokenManager,
createAccessTokenManager,
} from "../utils/jwt.token";
import { HttpCode, HttpMsg } from "../exceptions/http.codes.mgs";
import { HttpMsg } from "../exceptions/http.codes.mgs";
import ROLES from "../config/roles";

async function loginManager(
loginReq: LoginResquestManager
): Promise<LoginResponse> {
const role = ROLES.MANAGER;
class LoginController {
async loginManager(loginReq: LoginResquestManager): Promise<LoginResponse> {
const role = ROLES.MANAGER;

// check manager exists
const authExists = await checkAuthExistsForManager(
loginReq.userName,
loginReq.teamId
);
// check manager exists
const authExists = await authService.checkAuthExistsForManager(
loginReq.userName,
loginReq.teamId
);

if (!authExists) {
throw new Error(HttpMsg.AUTH_DOES_NOT_EXIST);
}
if (!authExists) {
throw new Error(HttpMsg.AUTH_DOES_NOT_EXIST);
}

// check auth
const isMatch = await checkAuthManager(
loginReq.userName,
loginReq.password,
loginReq.teamId
);
// check auth
const isMatch = await authService.checkAuthManager(
loginReq.userName,
loginReq.password,
loginReq.teamId
);

if (!isMatch) {
throw new Error(HttpMsg.PASSWORD_INCORRECT);
}
if (!isMatch) {
throw new Error(HttpMsg.PASSWORD_INCORRECT);
}

try {
// create refresh token
const refreshToken = createRefreshTokenManager(loginReq, role);
try {
// create refresh token
const refreshToken = createRefreshTokenManager(loginReq, role);

// create access token
const accessToken = createAccessTokenManager(loginReq, role);
// create access token
const accessToken = createAccessTokenManager(loginReq, role);

// return new LoginResponse(refreshToken, accessToken);
const loginResponse = new LoginResponse(refreshToken, accessToken);
// return new LoginResponse(refreshToken, accessToken);
const loginResponse = new LoginResponse(refreshToken, accessToken);

return loginResponse;
} catch (error) {
console.error(error);
throw new Error(HttpMsg.ERROR_CREATING_LOGIN);
return loginResponse;
} catch (error) {
console.error(error);
throw error;
}
}
}

async function loginPlayer(loginReq: LoginResquest): Promise<LoginResponse> {
const role = ROLES.PLAYER;
async loginPlayer(loginReq: LoginResquest): Promise<LoginResponse> {
const role = ROLES.PLAYER;

// check manager exists
const authExists = await checkAuthExists(loginReq.userName);
// check manager exists
const authExists = await authService.checkAuthExists(loginReq.userName);

if (!authExists) {
throw new Error(HttpMsg.AUTH_DOES_NOT_EXIST);
}
if (!authExists) {
throw new Error(HttpMsg.AUTH_DOES_NOT_EXIST);
}

// check auth
const isMatch = await checkAuth(loginReq.userName, loginReq.password);
// check auth
const isMatch = await authService.checkAuth(
loginReq.userName,
loginReq.password
);

if (!isMatch) {
throw new Error(HttpMsg.PASSWORD_INCORRECT);
}
if (!isMatch) {
throw new Error(HttpMsg.PASSWORD_INCORRECT);
}

try {
// create refresh token
const refreshToken = createRefreshToken(loginReq, role);
try {
// create refresh token
const refreshToken = createRefreshToken(loginReq, role);

// create access token
const accessToken = createAccessToken(loginReq, role);
// create access token
const accessToken = createAccessToken(loginReq, role);

// create jwt
const jwt = await createJwt(loginReq.userName, refreshToken);
// create jwt
const jwt = await jwtService.createJwt(loginReq.userName, refreshToken);

// return new LoginResponse(refreshToken, accessToken);
const loginResponse = new LoginResponse(refreshToken, accessToken);
// return new LoginResponse(refreshToken, accessToken);
const loginResponse = new LoginResponse(refreshToken, accessToken);

return loginResponse;
} catch (error) {
console.error(error);
throw new Error(HttpMsg.ERROR_CREATING_LOGIN);
return loginResponse;
} catch (error) {
console.error(error);
throw error;
}
}
}

async function logout(email: string): Promise<boolean> {
try {
const jwtExists = await checkJwtExists(email);
async logout(email: string): Promise<boolean> {
try {
const jwtExists = await jwtService.checkJwtExists(email);

if (jwtExists) {
await jwtService.deleteJwt(email);
}

if (jwtExists) {
await deleteJwt(email);
return true;
} catch (error) {
console.error(error);
throw error;
}

return true;
} catch (error) {
console.error(error);
throw new Error(HttpMsg.ERROR_CREATING_JWT);
return false;
}

return false;
}

export { loginManager, loginPlayer, logout };
export default new LoginController();
Loading

0 comments on commit 2f5401b

Please sign in to comment.