Browse Source

refactor api and remove unused code

pull/4/head
isra el 2 years ago
parent
commit
ea8fea3760
  1. 22
      api/src/app.controller.spec.ts
  2. 5
      api/src/app.controller.ts
  3. 6
      api/src/app.module.ts
  4. 4
      api/src/app.service.ts
  5. 2
      api/src/auth/auth.module.ts
  6. 4
      api/src/auth/auth.service.ts
  7. 16
      api/src/auth/guards/auth.guard.ts
  8. 2
      api/src/auth/guards/can-modify-api-key.guard.ts
  9. 7
      api/src/auth/jwt.strategy.ts
  10. 2
      api/src/auth/schemas/api-key.schema.ts
  11. 2
      api/src/gateway/gateway.controller.ts
  12. 4
      api/src/gateway/gateway.module.ts
  13. 2
      api/src/gateway/gateway.service.spec.ts
  14. 2
      api/src/gateway/gateway.service.ts
  15. 2
      api/src/gateway/guards/can-modify-device.guard.ts
  16. 2
      api/src/gateway/schemas/device.schema.ts
  17. 2
      api/src/gateway/schemas/sms.schema.ts

22
api/src/app.controller.spec.ts

@ -1,22 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing'
import { AppController } from './app.controller'
import { AppService } from './app.service'
describe('AppController', () => {
let appController: AppController
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile()
appController = app.get<AppController>(AppController)
})
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!')
})
})
})

5
api/src/app.controller.ts

@ -1,5 +0,0 @@
import { Controller, Get } from '@nestjs/common'
import { AppService } from './app.service'
@Controller()
export class AppController {}

6
api/src/app.module.ts

@ -1,6 +1,4 @@
import { Module } from '@nestjs/common' import { Module } from '@nestjs/common'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { MongooseModule } from '@nestjs/mongoose' import { MongooseModule } from '@nestjs/mongoose'
import { GatewayModule } from './gateway/gateway.module' import { GatewayModule } from './gateway/gateway.module'
import { AuthModule } from './auth/auth.module' import { AuthModule } from './auth/auth.module'
@ -13,7 +11,7 @@ import { UsersModule } from './users/users.module'
UsersModule, UsersModule,
GatewayModule, GatewayModule,
], ],
controllers: [AppController],
providers: [AppService],
controllers: [],
providers: [],
}) })
export class AppModule {} export class AppModule {}

4
api/src/app.service.ts

@ -1,4 +0,0 @@
import { Injectable } from '@nestjs/common'
@Injectable()
export class AppService {}

2
api/src/auth/auth.module.ts

@ -2,7 +2,7 @@ import { Module } from '@nestjs/common'
import { JwtModule } from '@nestjs/jwt' import { JwtModule } from '@nestjs/jwt'
import { MongooseModule } from '@nestjs/mongoose' import { MongooseModule } from '@nestjs/mongoose'
import { PassportModule } from '@nestjs/passport' import { PassportModule } from '@nestjs/passport'
import { UsersModule } from 'src/users/users.module'
import { UsersModule } from '../users/users.module'
import { AuthController } from './auth.controller' import { AuthController } from './auth.controller'
import { AuthService } from './auth.service' import { AuthService } from './auth.service'
import { JwtStrategy } from './jwt.strategy' import { JwtStrategy } from './jwt.strategy'

4
api/src/auth/auth.service.ts

@ -1,12 +1,12 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common' import { HttpException, HttpStatus, Injectable } from '@nestjs/common'
import { UsersService } from 'src/users/users.service'
import { UsersService } from '../users/users.service'
import { JwtService } from '@nestjs/jwt' import { JwtService } from '@nestjs/jwt'
import * as bcrypt from 'bcryptjs' import * as bcrypt from 'bcryptjs'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import { InjectModel } from '@nestjs/mongoose' import { InjectModel } from '@nestjs/mongoose'
import { ApiKey, ApiKeyDocument } from './schemas/api-key.schema' import { ApiKey, ApiKeyDocument } from './schemas/api-key.schema'
import { Model } from 'mongoose' import { Model } from 'mongoose'
import { User } from 'src/users/schemas/user.schema'
import { User } from '../users/schemas/user.schema'
import axios from 'axios' import axios from 'axios'
@Injectable() @Injectable()
export class AuthService { export class AuthService {

16
api/src/auth/guards/auth.guard.ts

@ -6,7 +6,7 @@ import {
Injectable, Injectable,
} from '@nestjs/common' } from '@nestjs/common'
import { JwtService } from '@nestjs/jwt' import { JwtService } from '@nestjs/jwt'
import { UsersService } from 'src/users/users.service'
import { UsersService } from '../../users/users.service'
import { AuthService } from '../auth.service' import { AuthService } from '../auth.service'
import * as bcrypt from 'bcryptjs' import * as bcrypt from 'bcryptjs'
@ -32,15 +32,13 @@ export class AuthGuard implements CanActivate {
// check apiKey in query params // check apiKey in query params
else if (request.query.apiKey) { else if (request.query.apiKey) {
const apiKeyStr = request.query.apiKey const apiKeyStr = request.query.apiKey
if (apiKeyStr) {
var regex = new RegExp(`^${apiKeyStr.substr(0, 17)}`, 'g')
const apiKey = await this.authService.findApiKey({
apiKey: { $regex: regex },
})
const regex = new RegExp(`^${apiKeyStr.substr(0, 17)}`, 'g')
const apiKey = await this.authService.findApiKey({
apiKey: { $regex: regex },
})
if (apiKey && bcrypt.compareSync(apiKeyStr, apiKey.hashedApiKey)) {
userId = apiKey.user
}
if (apiKey && bcrypt.compareSync(apiKeyStr, apiKey.hashedApiKey)) {
userId = apiKey.user
} }
} }

2
api/src/auth/guards/can-modify-api-key.guard.ts

@ -6,7 +6,7 @@ import {
Injectable, Injectable,
} from '@nestjs/common' } from '@nestjs/common'
import mongoose from 'mongoose' import mongoose from 'mongoose'
import { UserRole } from 'src/users/user-roles.enum'
import { UserRole } from '../../users/user-roles.enum'
import { AuthService } from '../auth.service' import { AuthService } from '../auth.service'
@Injectable() @Injectable()

7
api/src/auth/jwt.strategy.ts

@ -1,8 +1,8 @@
import { ExtractJwt, Strategy } from 'passport-jwt' import { ExtractJwt, Strategy } from 'passport-jwt'
import { PassportStrategy } from '@nestjs/passport' import { PassportStrategy } from '@nestjs/passport'
import { HttpException, HttpStatus, Injectable } from '@nestjs/common' import { HttpException, HttpStatus, Injectable } from '@nestjs/common'
import { UsersService } from 'src/users/users.service'
import { User } from 'src/users/schemas/user.schema'
import { UsersService } from '../users/users.service'
import { User } from '../users/schemas/user.schema'
@Injectable() @Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) { export class JwtStrategy extends PassportStrategy(Strategy) {
@ -19,8 +19,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
const user = await this.usersService.findOne({ _id: userId }) const user = await this.usersService.findOne({ _id: userId })
if (!user) { if (!user) {
throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED) throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED)
} else {
return user
} }
return user
} }
} }

2
api/src/auth/schemas/api-key.schema.ts

@ -1,6 +1,6 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose' import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document, Types } from 'mongoose' import { Document, Types } from 'mongoose'
import { User } from 'src/users/schemas/user.schema'
import { User } from '../../users/schemas/user.schema'
export type ApiKeyDocument = ApiKey & Document export type ApiKeyDocument = ApiKey & Document

2
api/src/gateway/gateway.controller.ts

@ -9,7 +9,7 @@ import {
Get, Get,
} from '@nestjs/common' } from '@nestjs/common'
import { ApiBearerAuth, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger' import { ApiBearerAuth, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger'
import { AuthGuard } from 'src/auth/guards/auth.guard'
import { AuthGuard } from '../auth/guards/auth.guard'
import { RegisterDeviceInputDTO, SendSMSInputDTO } from './gateway.dto' import { RegisterDeviceInputDTO, SendSMSInputDTO } from './gateway.dto'
import { GatewayService } from './gateway.service' import { GatewayService } from './gateway.service'
import { CanModifyDevice } from './guards/can-modify-device.guard' import { CanModifyDevice } from './guards/can-modify-device.guard'

4
api/src/gateway/gateway.module.ts

@ -3,8 +3,8 @@ import { MongooseModule } from '@nestjs/mongoose'
import { Device, DeviceSchema } from './schemas/device.schema' import { Device, DeviceSchema } from './schemas/device.schema'
import { GatewayController } from './gateway.controller' import { GatewayController } from './gateway.controller'
import { GatewayService } from './gateway.service' import { GatewayService } from './gateway.service'
import { AuthModule } from 'src/auth/auth.module'
import { UsersModule } from 'src/users/users.module'
import { AuthModule } from '../auth/auth.module'
import { UsersModule } from '../users/users.module'
@Module({ @Module({
imports: [ imports: [

2
api/src/gateway/gateway.service.spec.ts

@ -1,5 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing' import { Test, TestingModule } from '@nestjs/testing'
import { GatewayService } from './gateway.service' import { GatewayService } from './gateway.service'
import { AuthModule } from '../auth/auth.module'
describe('GatewayService', () => { describe('GatewayService', () => {
let service: GatewayService let service: GatewayService
@ -7,6 +8,7 @@ describe('GatewayService', () => {
beforeEach(async () => { beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
providers: [GatewayService], providers: [GatewayService],
imports: [AuthModule],
}).compile() }).compile()
service = module.get<GatewayService>(GatewayService) service = module.get<GatewayService>(GatewayService)

2
api/src/gateway/gateway.service.ts

@ -4,7 +4,7 @@ import { Device, DeviceDocument } from './schemas/device.schema'
import { Model } from 'mongoose' import { Model } from 'mongoose'
import * as firebaseAdmin from 'firebase-admin' import * as firebaseAdmin from 'firebase-admin'
import { RegisterDeviceInputDTO, SendSMSInputDTO } from './gateway.dto' import { RegisterDeviceInputDTO, SendSMSInputDTO } from './gateway.dto'
import { User } from 'src/users/schemas/user.schema'
import { User } from '../users/schemas/user.schema'
@Injectable() @Injectable()
export class GatewayService { export class GatewayService {
constructor( constructor(

2
api/src/gateway/guards/can-modify-device.guard.ts

@ -6,7 +6,7 @@ import {
Injectable, Injectable,
} from '@nestjs/common' } from '@nestjs/common'
import mongoose from 'mongoose' import mongoose from 'mongoose'
import { UserRole } from 'src/users/user-roles.enum'
import { UserRole } from '../../users/user-roles.enum'
import { GatewayService } from '../gateway.service' import { GatewayService } from '../gateway.service'
@Injectable() @Injectable()

2
api/src/gateway/schemas/device.schema.ts

@ -1,6 +1,6 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose' import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document, Types } from 'mongoose' import { Document, Types } from 'mongoose'
import { User } from 'src/users/schemas/user.schema'
import { User } from '../../users/schemas/user.schema'
export type DeviceDocument = Device & Document export type DeviceDocument = Device & Document

2
api/src/gateway/schemas/sms.schema.ts

@ -1,7 +1,5 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose' import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document, Types } from 'mongoose' import { Document, Types } from 'mongoose'
import { ApiKey } from 'src/auth/schemas/api-key.schema'
import { User } from 'src/users/schemas/user.schema'
import { Device } from './device.schema' import { Device } from './device.schema'
export type SMSDocument = SMS & Document export type SMSDocument = SMS & Document

Loading…
Cancel
Save