Skip to content

Commit

Permalink
other: solved conflicr
Browse files Browse the repository at this point in the history
  • Loading branch information
paga16-hash committed Jan 29, 2024
2 parents d5bb5f6 + 208a234 commit b7df79b
Show file tree
Hide file tree
Showing 32 changed files with 313 additions and 144 deletions.
18 changes: 14 additions & 4 deletions auth/db/auth-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ db = new Mongo().getDB('auth')

db.createCollection('user')
//TODO to test
db.user.insert([
db.user.insertMany([
{
name: 'Mattia',
surname: 'Matteini',
username: 'mattia',
password: '$2a$10$QmASVIA1cy65TArhkhINte52vrNuJMlSpdO2FVLqI/OM32LmK6jHS',
token: '',
refreshToken: '',
contacts: [{ type: 'SMS', value: '33344455678' }],
deviceIds: [
{ type: 'CAMERA', code: 'cam-02' },
{ type: 'SENSOR', code: 'sen-02' }
]
},
{
_id: {
$oid: '6582b78ee645d6402a3be6e2'
},
name: 'Mario',
surname: 'Rossi',
username: 'paga16',
Expand Down
1 change: 0 additions & 1 deletion auth/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ services:
- ${AUTH_PORT}:${AUTH_PORT}
depends_on:
- revue-auth-db
- ${KAFKA_CONTAINER}

revue-auth-db:
image: mongo
Expand Down
28 changes: 26 additions & 2 deletions auth/src/controller/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@ import { userSchema } from '@storage/monitoring/schemas/UserSchema.js'
import { UserRepository } from '@domain/monitoring/repository/UserRepository.js'
import { UserRepositoryImpl } from '@storage/monitoring/UserRepositoryImpl.js'
import { User } from '@domain/monitoring/core/User.js'
import { Contact } from '@domain/monitoring/core/Contact.js'
import { UserFactory } from '@domain/monitoring/factories/UserFactory.js'
import { UserFactoryImpl } from '@domain/monitoring/factories/impl/UserFactoryImpl.js'
import { DeviceIdFactory } from '@domain/device/factories/DeviceIdFactory.js'
import { DeviceIdFactoryImpl } from '@domain/device/factories/impl/DeviceIdFactoryImpl.js'
import { DeviceId } from '@domain/device/core/DeviceId.js'
import { DeviceTypeConverter } from '@utils/DeviceTypeConverter.js'
import { ContactFactory } from '@domain/monitoring/factories/ContactFactory.js'
import { ContactFactoryImpl } from '@domain/monitoring/factories/impl/ContactFactoryImpl.js'
import { ContactTypeConverter } from '@utils/ContactTypeConverter.js'

export const userModel: Model<User> = model<User>('User', userSchema, 'user')
const userManager: UserRepository = new UserRepositoryImpl(userModel)
const userFactory: UserFactory = new UserFactoryImpl()
const deviceIdFactory: DeviceIdFactory = new DeviceIdFactoryImpl()
const contactFactory: ContactFactory = new ContactFactoryImpl()

export const userController = {
getUserById: async (id: string): Promise<User> => {
Expand All @@ -19,6 +29,20 @@ export const userController = {
return await userManager.getUsers()
},
createUser: async (req: Request): Promise<void> => {
const contacts: Contact[] = req.body.contacts.map(
(contactObj: { value: string; type: string }) =>
contactFactory.createContact(
contactObj.value,
ContactTypeConverter.convertToContactType(contactObj.type)
)
)
const deviceIds: DeviceId[] = req.body.deviceIds.map(
(deviceIdObj: { type: string; code: string }) =>
deviceIdFactory.createId(
DeviceTypeConverter.convertToDeviceType(deviceIdObj.type),
deviceIdObj.code
)
)
const user: User = userFactory.createUser(
req.body.id,
req.body.name,
Expand All @@ -27,8 +51,8 @@ export const userController = {
req.body.password,
req.body.token,
req.body.refreshToken,
req.body.contacts,
req.body.deviceIds
contacts,
deviceIds
)
return await userManager.insertUser(user)
},
Expand Down
15 changes: 7 additions & 8 deletions auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import type { Express, NextFunction, Request, Response } from 'express'
import express from 'express'

import cors from 'cors'
import { config } from 'dotenv'
import mongoose from 'mongoose'
import { userAccessRouter } from './routes/userAccess.js'
import { userRouter } from './routes/user.js'
import { jwtManager } from './utils/JWTManager.js'
import HttpStatusCode from './utils/HttpStatusCode.js'
import cors from 'cors'

config()

export const app: Express = express()

app.use(express.json())
app.use(cors())

config()

const PORT: number = Number(process.env.PORT) || 4000
const PORT: number = Number(process.env.AUTH_PORT) || 4000

app.use((req: Request, res: Response, next: NextFunction) => {
const authHeader = req.headers.authorization
const token = authHeader && authHeader.split(' ')[1]

if (token === process.env.DEV_API_KEY) return next()
if ((req.url === '/login' || req.url === '/newToken') && req.method === 'POST') return next()
if ((req.url === '/login' || req.url === '/newToken') && req.method === 'POST') {
return next()
}
if (token === undefined)
return res.status(HttpStatusCode.FORBIDDEN).send({ error: 'No authentication token' })
else {
Expand Down Expand Up @@ -59,7 +58,7 @@ const mongoConnect = async (): Promise<void> => {
if (process.env.NODE_ENV !== 'test') {
//const server =
app.listen(PORT, async (): Promise<void> => {
console.log(`Auth server listening on http://${process.env.DB_HOST}:${PORT}`)
console.log(`Auth server listening on port ${PORT}`)
await mongoConnect()
})
}
3 changes: 1 addition & 2 deletions auth/src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ userRouter.route('/').get((req: Request, res: Response): void => {
.then((users: User[]): void => {
res.status(HttpStatusCode.OK).send(users)
})
.catch((err): void => {
console.log(err)
.catch((): void => {
res.send({ error: 'No user found' })
})
})
Expand Down
2 changes: 1 addition & 1 deletion auth/src/routes/userAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const userAccessRouter: Router = express.Router()
userAccessRouter.route('/login').post((req: Request, res: Response): void => {
userAccessController
.login(req.body.username, req.body.password)
.then((access: any): void => {
.then((access: { accessToken: string; refreshToken: string }): void => {
res.status(HttpStatusCode.OK).send(access)
})
.catch((err): void => {
Expand Down
58 changes: 30 additions & 28 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,39 @@ plugins {

subprojects {

apply(plugin = "com.github.node-gradle.node")
if (project.name != "kafka") {
apply(plugin = "com.github.node-gradle.node")

node {
download = false
}
class Task(val name: String, val args: List<String>, val dependencies: List<String> = listOf())

listOf(
Task("install", listOf("install")),
Task("build", listOf("run", "build")),
Task("start", listOf("start")),
Task("test", listOf("run", "test")),
Task("format", listOf("run", "format")),
Task("format-fix", listOf("run", "format:fix")),
Task("lint", listOf("run", "lint"))
).forEach { task ->
tasks.register<NpmTask>(task.name) {
args = task.args
if (task.name != "install") dependsOn(":${project.name}:install")
if (project.name != "domain") dependsOn(":domain:build")
node {
download = false
}
class Task(val name: String, val args: List<String>, val dependencies: List<String> = listOf())

listOf(
Task("install", listOf("install")),
Task("build", listOf("run", "build")),
Task("start", listOf("start")),
Task("test", listOf("run", "test")),
Task("format", listOf("run", "format")),
Task("format-fix", listOf("run", "format:fix")),
Task("lint", listOf("run", "lint"))
).forEach { task ->
tasks.register<NpmTask>(task.name) {
args = task.args
if (task.name != "install") dependsOn(":${project.name}:install")
if (project.name != "domain") dependsOn(":domain:build")
}
}
}

// ordering task execution
if (project.name != "domain") {
tasks.findByPath(":${project.name}:install")?.mustRunAfter(":domain:build")
}
tasks.findByPath(":${project.name}:build")?.mustRunAfter(":${project.name}:install")
// ordering task execution
if (project.name != "domain") {
tasks.findByPath(":${project.name}:install")?.mustRunAfter(":domain:build")
}
tasks.findByPath(":${project.name}:build")?.mustRunAfter(":${project.name}:install")

tasks.register("clean", Delete::class) {
// Specify the directories to be deleted
delete("dist", "node_modules/domain", "tsconfig.tsbuildinfo")
tasks.register("clean", Delete::class) {
delete("dist", "node_modules/domain", "tsconfig.tsbuildinfo")
}
}

}
26 changes: 13 additions & 13 deletions camera/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ services:
depends_on:
- ${KAFKA_CONTAINER}

revue-camera-3:
image: revue-camera
container_name: revue-camera-3
build:
context: .
dockerfile: ./camera/Dockerfile
restart: on-failure
environment:
- CAMERA_CODE=cam-03
ports:
- ${CAMERA_PORT_3}:${CAMERA_PORT_3}
depends_on:
- ${KAFKA_CONTAINER}
# revue-camera-3:
# image: revue-camera
# container_name: revue-camera-3
# build:
# context: .
# dockerfile: ./camera/Dockerfile
# restart: on-failure
# environment:
# - CAMERA_CODE=cam-03
# ports:
# - ${CAMERA_PORT_3}:${CAMERA_PORT_3}
# depends_on:
# - ${KAFKA_CONTAINER}
43 changes: 43 additions & 0 deletions compose-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# Function to display usage information
usage() {
echo "Usage: $0 (--up | --down)"
exit 1
}

# Check if at least two argument is provided
if [ "$#" -lt 1 ]; then
usage
fi

command=''
# Process options
while [[ $# -gt 0 ]]; do
case "$1" in
--up|--down)
command="$1"
shift
;;
# *)
# echo "$1"
# services+=("$1")
# shift
# ;;
esac
done

# Check if --command option is provided
if [ -z "$command" ]; then
usage
fi

compose_files=("-fauth/docker-compose.yml" "-fkafka/docker-compose.yml" "-fmonitoring/docker-compose.yml" "-falarm/docker-compose.yml"
"-ffrontend/docker-compose.yml" "-fcamera/docker-compose.yml")


if [ "$command" == "--down" ]; then
docker compose --project-name revue --project-directory . "${compose_files[@]}" "${command:2}" -v
else
docker compose --project-name revue --project-directory . "${compose_files[@]}" "${command:2}" -d --build
fi
6 changes: 1 addition & 5 deletions domain/src/domain/monitoring/core/Contact.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { ContactType } from './impl/ContactType.js'
import { ContactType } from './impl/enum/ContactType.js'

export interface Contact {
get id(): string

set id(id: string)

get value(): string

set value(v: string)
Expand Down
11 changes: 2 additions & 9 deletions domain/src/domain/monitoring/core/impl/ContactImpl.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { Contact } from '../Contact.js'
import { ContactType } from './ContactType.js'
import { ContactType } from './enum/ContactType.js'

export class ContactImpl implements Contact {
private _id: string
private _value: string
private _contactType: ContactType

constructor(id: string, value: string, contactType: ContactType) {
this._id = id
constructor(value: string, contactType: ContactType) {
this._value = value
this._contactType = contactType
}

get id(): string {
return this._value
}

get value(): string {
return this._value
}
Expand Down
2 changes: 1 addition & 1 deletion domain/src/domain/monitoring/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export * from './MonitoringRoom'
export * from './impl/ContactImpl'
export * from './impl/UserImpl'
export * from './impl/MonitoringRoomImpl'
export * from './impl/ContactType'
export * from './impl/enum/ContactType'
4 changes: 2 additions & 2 deletions domain/src/domain/monitoring/factories/ContactFactory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Contact } from '../core/Contact.js'
import { ContactType } from '../core/impl/ContactType.js'
import { ContactType } from '../core/impl/enum/ContactType.js'

export interface ContactFactory {
createContact(id: string, value: string, type: ContactType): Contact
createContact(value: string, type: ContactType): Contact
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Contact } from '../../core/Contact.js'
import { ContactFactory } from '../ContactFactory.js'
import { ContactImpl } from '../../core/impl/ContactImpl.js'
import { ContactType } from '../../core/impl/ContactType.js'
import { ContactType } from '../../core/impl/enum/ContactType.js'

export class ContactFactoryImpl implements ContactFactory {
createContact(id: string, value: string, type: ContactType): Contact {
return new ContactImpl(id, value, type)
createContact(value: string, type: ContactType): Contact {
return new ContactImpl(value, type)
}
}
20 changes: 17 additions & 3 deletions domain/src/storage/monitoring/UserRepositoryImpl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import mongoose, { Model } from 'mongoose'
import { User } from '../../domain/monitoring/core/User.js'
import { UserRepository } from '../../domain/monitoring/repository/UserRepository.js'
import { ContactTypeConverter } from '../../utils/ContactTypeConverter.js'
import { DeviceTypeConverter } from '../../utils/DeviceTypeConverter.js'
import { Contact } from '../../domain/monitoring/core/Contact.js'
import { DeviceId } from '../../domain/device/core/DeviceId.js'

export class UserRepositoryImpl implements UserRepository {
userModel: Model<User>
Expand Down Expand Up @@ -30,10 +34,20 @@ export class UserRepositoryImpl implements UserRepository {
password: user.password,
token: user.token,
refreshToken: user.refreshToken,
contacts: user.contacts,
deviceIds: user.deviceIds
contacts: user.contacts.map((contact: Contact) => {
return {
type: ContactTypeConverter.convertToString(contact.contactType),
value: contact.value.toString()
}
}),
deviceIds: user.deviceIds.map((deviceId: DeviceId) => {
return {
type: DeviceTypeConverter.convertToString(deviceId.type),
code: deviceId.code
}
})
})
.catch((err) => {
.catch((err): void => {
throw err
})
}
Expand Down
Loading

0 comments on commit b7df79b

Please sign in to comment.