Browse Source

refactor(api): clean up code

pull/1/head
isra el 3 years ago
parent
commit
eb112252a0
  1. 4
      api/src/app.service.ts
  2. 10
      api/src/auth/auth.service.ts
  3. 2
      api/src/auth/guards/auth.guard.ts
  4. 17
      api/src/gateway/gateway.service.ts

4
api/src/app.service.ts

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

10
api/src/auth/auth.service.ts

@ -16,14 +16,6 @@ export class AuthService {
@InjectModel(ApiKey.name) private apiKeyModel: Model<ApiKeyDocument>,
) {}
async validateUser(_id: string): Promise<User | null> {
const user = await this.usersService.findOne({ _id })
if (user) {
return user
}
return null
}
async login(userData: any) {
const user = await this.usersService.findOne({ email: userData.email })
if (!user) {
@ -101,7 +93,7 @@ export class AuthService {
const hashedApiKey = await bcrypt.hash(apiKey, 10)
const newApiKey = new this.apiKeyModel({
apiKey: apiKey.substr(0, 17) + '******************',
apiKey: apiKey.substr(0, 17) + '*'.repeat(18),
hashedApiKey,
user: currentUser._id,
})

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

@ -45,7 +45,7 @@ export class AuthGuard implements CanActivate {
}
if (userId) {
const user = await this.authService.validateUser(userId)
const user = await this.usersService.findOne({ _id: userId })
if (user) {
request.user = user
return true

17
api/src/gateway/gateway.service.ts

@ -61,30 +61,17 @@ export class GatewayService {
async sendSMS(deviceId: string, smsData: SendSMSInputDTO): Promise<any> {
const device = await this.deviceModel.findById(deviceId)
if (!device) {
throw new HttpException(
{
error: 'Device not found',
},
HttpStatus.NOT_FOUND,
)
}
if (!device.enabled) {
if (!device?.enabled) {
throw new HttpException(
{
success: false,
error: 'Device is disabled',
error: 'Device does not exist or is not enabled',
},
HttpStatus.BAD_REQUEST,
)
}
const payload: any = {
// notification: {
// title: 'SMS',
// body: 'message',
// },
data: {
smsData: JSON.stringify(smsData),
},

Loading…
Cancel
Save