Skip to content

Commit

Permalink
feat(#19): view current location as a driver i want to see my current…
Browse files Browse the repository at this point in the history
… location on a map in real time so that i can track my position and navigate the area effectively

- refactor docker (separate mqtt listener and api cause they don't need to be together)
- moved docker to root directory
- using docker base to reuse common code
- basic nearby drivers util
  • Loading branch information
chriscoderdr committed Nov 1, 2024
1 parent 29584ed commit 0a1a56d
Show file tree
Hide file tree
Showing 24 changed files with 1,023 additions and 141 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
**/node_modules
.yarn
.yarnrc.yml
dist
.git
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ Thumbs.db

.nx/cache
.nx/workspace-data
apps/api/logs/app.log
11 changes: 11 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"trailingComma": "none",
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"endOfLine": "lf",
"printWidth": 80,
"proseWrap": "never",
"trimTrailingWhitespace": true
}
9 changes: 9 additions & 0 deletions Dockerfile.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# syntax = edrevo/dockerfile-plus

INCLUDE+ Dockerfile.base

EXPOSE 3000

WORKDIR /app/apps/api

CMD ["yarn", "dev"]
14 changes: 14 additions & 0 deletions Dockerfile.base
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM node:18-alpine

RUN apk add --no-cache git

WORKDIR /app

COPY package.json yarn.lock ./

COPY . .

WORKDIR /app/apps/api

RUN yarn install --frozen-lockfile

9 changes: 9 additions & 0 deletions Dockerfile.mqtt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# syntax = edrevo/dockerfile-plus

INCLUDE+ Dockerfile.base

EXPOSE 1883

WORKDIR /app/apps/api

CMD ["yarn", "start-mqtt"]
3 changes: 0 additions & 3 deletions apps/api/.dockerignore

This file was deleted.

14 changes: 0 additions & 14 deletions apps/api/Dockerfile

This file was deleted.

58 changes: 0 additions & 58 deletions apps/api/docker-compose.yml

This file was deleted.

10 changes: 7 additions & 3 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"test": "mocha -r ts-node/register test/**/*.test.ts",
"build": "tsc",
"start": "node --es-module-specifier-resolution=node",
"dev": "concurrently \"yarn build\" \"nodemon --es-module-specifier-resolution=node\""
"dev": "tsx watch src/start.ts",
"start-mqtt": "tsx watch src/start-mqtt.ts"
},
"keywords": [],
"author": "",
Expand All @@ -19,12 +20,14 @@
"ioredis": "^5.4.1",
"koa": "^2.15.3",
"koa-bodyparser": "^4.4.1",
"mqtt": "^5.10.1",
"pg": "^8.13.1",
"pg-hstore": "^2.3.4",
"sequelize": "^6.37.5",
"ts-node": "^10.9.2",
"twilio": "^5.3.5",
"typescript": "^5.6.3"
"typescript": "^5.6.3",
"winston": "^3.15.0"
},
"devDependencies": {
"@types/bcrypt": "^5.0.2",
Expand All @@ -44,6 +47,7 @@
"eslint": "^9.13.0",
"mocha": "^10.7.3",
"nodemon": "^3.1.7",
"supertest": "^7.0.0"
"supertest": "^7.0.0",
"tsx": "^4.19.2"
}
}
7 changes: 4 additions & 3 deletions apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import sequelize from './config/database';
import driverRoutes from './routes';
import logger from './utils/logger';

const app = new Koa();

sequelize.sync().then(() => console.log("Connected to PostgreSQL"));
sequelize.sync().then(() => logger.info('Connected to PostgreSQL'));

app.use(bodyParser());

// Temporary test route
// TODO: remove this route
app.use(async (ctx, next) => {
if (ctx.path === '/') {
ctx.body = 'Server is up!';
} else {
console.log('Request received:', ctx.method, ctx.path);
logger.info('Request received:', ctx.method, ctx.path);
await next();
}
});
Expand Down
7 changes: 4 additions & 3 deletions apps/api/src/controllers/driver-controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Context } from 'koa';
import { Op } from 'sequelize';
import Driver from '../models/driver_tmp';
import Driver from '../models/driver';
import logger from '../utils/logger';

export const registerDriver = async (ctx: Context) => {
const { name, email, phone, password } = ctx.request.body as {
Expand All @@ -9,10 +10,10 @@ export const registerDriver = async (ctx: Context) => {
phone: string;
password: string;
};
console.log('registerDriver', name, email, phone, password);
logger.info('registerDriver', name, email, phone, password);

if (!name || !email || !phone || !password) {
console.log(`registerDriver: ${name}, ${email}, ${phone}, ${password}`);
logger.info(`registerDriver: ${name}, ${email}, ${phone}, ${password}`);
ctx.status = 400;
ctx.body = { error: `registerDriver: ${name}, ${email}, ${phone}, ${password}`};
return;
Expand Down
60 changes: 40 additions & 20 deletions apps/api/src/models/driver_tmp.ts → apps/api/src/models/driver.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import bcrypt from "bcrypt";
import { DataTypes, Model, Optional } from "sequelize";
import sequelize from "../config/database";
import bcrypt from 'bcrypt';
import { DataTypes, Model, Optional } from 'sequelize';
import sequelize from '../config/database';

const saltRounds = parseInt(process.env.BCRYPT_SALT_ROUNDS || "10", 10);
const saltRounds = parseInt(process.env.BCRYPT_SALT_ROUNDS || '10', 10);

interface DriverAttributes {
id: number;
Expand All @@ -13,11 +13,14 @@ interface DriverAttributes {
createdAt?: Date;
updatedAt?: Date;
loginAt?: Date;
lastLocationLatitude?: number;
lastLocationLongitude?: number;
lastLocationUpdatedAt?: Date;
isAvailable?: boolean;
}

interface DriverCreationAttributes extends Optional<DriverAttributes, 'id'> {}


class Driver extends Model<DriverAttributes, DriverCreationAttributes> {
public async comparePassword(password: string): Promise<boolean> {
return bcrypt.compare(password, this.dataValues.password);
Expand All @@ -29,54 +32,71 @@ Driver.init(
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: false,
allowNull: false
},
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
validate: { isEmail: true },
validate: { isEmail: true }
},
phone: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
unique: true
},
password: {
type: DataTypes.STRING,
allowNull: false,
allowNull: false
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
defaultValue: DataTypes.NOW
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
defaultValue: DataTypes.NOW
},
loginAt: {
type: DataTypes.DATE,
allowNull: true,
allowNull: true
},
lastLocationLatitude: {
type: DataTypes.FLOAT,
allowNull: true
},
lastLocationLongitude: {
type: DataTypes.FLOAT,
allowNull: true
},
lastLocationUpdatedAt: {
type: DataTypes.DATE,
allowNull: true
},
isAvailable: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true
}
},
{
sequelize,
tableName: "drivers",
tableName: 'drivers',
hooks: {
beforeCreate: async (driver) => {
console.error('driver', driver)
console.error('password: ', driver.dataValues.password);
console.error('driver.salt', saltRounds);
driver.dataValues.password = await bcrypt.hash(driver.dataValues.password, saltRounds);
},
driver.dataValues.password = await bcrypt.hash(
driver.dataValues.password,
saltRounds
);
}
},
timestamps: true,
timestamps: true
}
);

Expand Down
Loading

0 comments on commit 0a1a56d

Please sign in to comment.