API-Explorer-II/server/controllers/RequestController.ts

166 lines
5.3 KiB
TypeScript
Raw Normal View History

2024-05-14 11:41:52 +00:00
/*
* Open Bank Project - API Explorer II
* Copyright (C) 2023-2024, TESOBE GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Email: contact@tesobe.com
* TESOBE GmbH
* Osloerstrasse 16/17
* Berlin 13359, Germany
*
* This product includes software developed at
* TESOBE (http://www.tesobe.com/)
*
*/
2023-06-05 15:32:25 +00:00
import { Controller, Session, Req, Res, Get, Delete, Post, Put } from 'routing-controllers'
2023-05-03 17:17:01 +00:00
import { Request, Response } from 'express'
import OBPClientService from '../services/OBPClientService'
2025-12-01 10:07:05 +00:00
import { Service, Container } from 'typedi'
2023-05-03 17:17:01 +00:00
@Service()
@Controller()
export class OBPController {
2025-12-01 10:07:05 +00:00
private obpClientService: OBPClientService
constructor() {
// Explicitly get OBPClientService from the container to avoid injection issues
this.obpClientService = Container.get(OBPClientService)
}
2023-05-03 17:17:01 +00:00
@Get('/get')
2023-06-05 15:32:25 +00:00
async get(@Session() session: any, @Req() request: Request, @Res() response: Response): Response {
2023-05-03 17:17:01 +00:00
const path = request.query.path
2023-06-05 15:32:25 +00:00
const oauthConfig = session['clientConfig']
2025-12-01 10:28:40 +00:00
2025-12-02 00:11:17 +00:00
// Debug logging
console.log('RequestController.get - Path:', path)
console.log('RequestController.get - Has session:', !!session)
console.log('RequestController.get - Has clientConfig:', !!oauthConfig)
console.log('RequestController.get - Has oauth2:', !!oauthConfig?.oauth2)
console.log('RequestController.get - Has accessToken:', !!oauthConfig?.oauth2?.accessToken)
console.log('RequestController.get - Session keys:', Object.keys(session || {}))
2025-12-01 10:28:40 +00:00
// Check if user is authenticated
if (!oauthConfig || !oauthConfig.oauth2?.accessToken) {
2025-12-02 00:11:17 +00:00
console.log('RequestController.get - User not authenticated')
2025-12-01 10:28:40 +00:00
return response.status(401).json({
code: 401,
message: 'OBP-20001: User not logged in. Authentication is required!'
})
}
try {
const result = await this.obpClientService.get(path, oauthConfig)
return response.json(result)
} catch (error: any) {
console.error('RequestController.get error:', error)
return response.status(error.status || 500).json({
code: error.status || 500,
message: error.message || 'Internal server error'
})
}
2023-05-03 17:17:01 +00:00
}
@Post('/create')
2023-06-05 15:32:25 +00:00
async create(
@Session() session: any,
@Req() request: Request,
@Res() response: Response
): Response {
2023-05-03 17:17:01 +00:00
const path = request.query.path
const data = request.body
2023-06-05 15:32:25 +00:00
const oauthConfig = session['clientConfig']
2025-12-01 10:28:40 +00:00
// Check if user is authenticated
if (!oauthConfig || !oauthConfig.oauth2?.accessToken) {
return response.status(401).json({
code: 401,
message: 'OBP-20001: User not logged in. Authentication is required!'
})
}
try {
const result = await this.obpClientService.create(path, data, oauthConfig)
return response.json(result)
} catch (error: any) {
console.error('RequestController.create error:', error)
return response.status(error.status || 500).json({
code: error.status || 500,
message: error.message || 'Internal server error'
})
}
2023-05-03 17:17:01 +00:00
}
@Put('/update')
2023-06-05 15:32:25 +00:00
async update(
@Session() session: any,
@Req() request: Request,
@Res() response: Response
): Response {
2023-05-03 17:17:01 +00:00
const path = request.query.path
const data = request.body
2023-06-05 15:32:25 +00:00
const oauthConfig = session['clientConfig']
2025-12-01 10:28:40 +00:00
// Check if user is authenticated
if (!oauthConfig || !oauthConfig.oauth2?.accessToken) {
return response.status(401).json({
code: 401,
message: 'OBP-20001: User not logged in. Authentication is required!'
})
}
try {
const result = await this.obpClientService.update(path, data, oauthConfig)
return response.json(result)
} catch (error: any) {
console.error('RequestController.update error:', error)
return response.status(error.status || 500).json({
code: error.status || 500,
message: error.message || 'Internal server error'
})
}
2023-05-03 17:17:01 +00:00
}
@Delete('/delete')
2023-06-05 15:32:25 +00:00
async delete(
@Session() session: any,
@Req() request: Request,
@Res() response: Response
): Response {
2023-05-03 17:17:01 +00:00
const path = request.query.path
2023-06-05 15:32:25 +00:00
const oauthConfig = session['clientConfig']
2025-12-01 10:28:40 +00:00
// Check if user is authenticated
if (!oauthConfig || !oauthConfig.oauth2?.accessToken) {
return response.status(401).json({
code: 401,
message: 'OBP-20001: User not logged in. Authentication is required!'
})
}
try {
const result = await this.obpClientService.discard(path, oauthConfig)
return response.json(result)
} catch (error: any) {
console.error('RequestController.delete error:', error)
return response.status(error.status || 500).json({
code: error.status || 500,
message: error.message || 'Internal server error'
})
}
2023-05-03 17:17:01 +00:00
}
}