Skip to content

Commit 7efd963

Browse files
feat: implement StorX credential management and service integration
1 parent 16c4ec0 commit 7efd963

File tree

11 files changed

+263
-241
lines changed

11 files changed

+263
-241
lines changed

backend/src/modules/device/controllers/storx.controller.ts

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1-
import { Controller, Get, HttpCode, Post, Query } from '@nestjs/common';
2-
import { ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
1+
import {
2+
Body,
3+
Controller,
4+
Get,
5+
HttpCode,
6+
Post,
7+
Query,
8+
Request,
9+
Res,
10+
UseGuards,
11+
} from '@nestjs/common';
12+
import {
13+
ApiBearerAuth,
14+
ApiOperation,
15+
ApiQuery,
16+
ApiTags,
17+
} from '@nestjs/swagger';
318
import { StorxService } from '../services/storx.service';
19+
import { Response } from 'express';
20+
import { InsertStorxDto } from '../data-transfer-objects/insert-storx.dto';
21+
import { JwtAuthGuard } from 'src/modules/authentication/guard/jwt-auth.guard';
422

523
@ApiTags('StorX')
624
@Controller('app')
@@ -19,12 +37,27 @@ export class StorXController {
1937
};
2038
}
2139

22-
@Get('v1/storx/callback')
40+
@Post('v1/storx/credentials')
2341
@HttpCode(200)
24-
async callback(@Query('access_grant') accessGrant: string) {
25-
await this.storxService.handleCallback(accessGrant);
26-
return {
27-
message: 'Callback received',
28-
};
42+
@ApiOperation({
43+
summary: 'Save StorX Credentials',
44+
description: 'Save StorX Credentials',
45+
})
46+
@UseGuards(JwtAuthGuard)
47+
@ApiBearerAuth()
48+
async saveCredentials(@Body() body: InsertStorxDto, @Request() request) {
49+
return this.storxService.saveCredentials(body, request.user.userId);
50+
}
51+
52+
@Get('v1/storx/credentials')
53+
@HttpCode(200)
54+
@ApiOperation({
55+
summary: 'Get StorX Credentials',
56+
description: 'Get StorX Credentials',
57+
})
58+
@UseGuards(JwtAuthGuard)
59+
@ApiBearerAuth()
60+
async getCredentials(@Request() request) {
61+
return this.storxService.getCredentials(request.user.userId);
2962
}
3063
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsString } from 'class-validator';
3+
4+
export class InsertStorxDto {
5+
@IsString({ message: 'accessGrant must be string.' })
6+
@ApiProperty({ required: true })
7+
accessGrant: string;
8+
}

backend/src/modules/device/device.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,18 @@ import { BuildingModule } from '../building/building.module';
2222
import { StorxService } from './services/storx.service';
2323
import { StorXController } from './controllers/storx.controller';
2424
import { JwtModule } from '@nestjs/jwt';
25+
import { storxFeature } from './features/storx.feature';
26+
import { StorxRepository } from './repositories/storx.repository';
2527

2628
@Module({
2729
imports: [
2830
JwtModule.register({
29-
secret: '$2a$10$4THkK2FfT2Y7AIbU0pBFPOpmKeTfrzs8On2Qe8L362D4DR8VI52gO',
31+
secret: process.env.STORX_CLIENT_SECRET,
3032
}),
3133
MongooseModule.forFeature(deviceFeature),
3234
MongooseModule.forFeature(deviceLogFeature),
3335
MongooseModule.forFeature(deviceTypeFeature),
36+
MongooseModule.forFeature(storxFeature),
3437
forwardRef(() => UserModule),
3538
forwardRef(() => NotificationModule),
3639
forwardRef(() => ServiceModule),
@@ -47,6 +50,7 @@ import { JwtModule } from '@nestjs/jwt';
4750
DeviceTypeService,
4851
DeviceTypeRepository,
4952
StorxService,
53+
StorxRepository,
5054
],
5155
controllers: [
5256
DeviceController,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { storxSchema } from '../schemas/storx.schema';
2+
3+
export const storxFeature = [{ name: 'storx', schema: storxSchema }];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Document } from 'mongoose';
2+
3+
export interface Storx extends Document {
4+
accessGrant: string;
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Model } from 'mongoose';
2+
import { Storx } from '../interfaces/storx.interface';
3+
4+
export interface StorxModel extends Model<Storx> {
5+
[x: string]: any;
6+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { InjectModel } from '@nestjs/mongoose';
2+
import { StorxModel } from '../models/storx.model';
3+
import { Injectable } from '@nestjs/common';
4+
5+
@Injectable()
6+
export class StorxRepository {
7+
constructor(
8+
@InjectModel('storx')
9+
private readonly storxModel: StorxModel,
10+
) {}
11+
12+
async insertStorx(data) {
13+
return await this.storxModel.create(data);
14+
}
15+
16+
async getStorxByUserId(userId: string) {
17+
return await this.storxModel.findOne().where('userId').equals(userId);
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Schema } from 'mongoose';
2+
3+
const storxSchema = new Schema({
4+
userId: { type: String, required: true },
5+
accessGrant: { type: String, required: true },
6+
accessKeyId: { type: String },
7+
secretKey: { type: String },
8+
endpoint: { type: String },
9+
});
10+
11+
export { storxSchema };

0 commit comments

Comments
 (0)