68 lines
1.6 KiB
TypeScript
68 lines
1.6 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Headers,
|
|
Inject,
|
|
Param,
|
|
Post,
|
|
Res,
|
|
UseGuards,
|
|
} from "@nestjs/common";
|
|
import { Response } from "express";
|
|
import { QueryHandlerService } from "./query.handler.service";
|
|
import { ApiTokenGuard } from "src/api/guards/api-token.guard";
|
|
import { QueryExecuterService } from "../executer/query.executer.service";
|
|
|
|
@Controller("query")
|
|
@UseGuards(ApiTokenGuard)
|
|
export class QueryController {
|
|
constructor(
|
|
@Inject(QueryHandlerService)
|
|
private readonly queryHandlerService: QueryHandlerService,
|
|
@Inject(QueryExecuterService)
|
|
private readonly queryExecuterService: QueryExecuterService
|
|
) {}
|
|
|
|
@Post("create")
|
|
async createQuery(
|
|
@Body() queryData: { projectToken: string; source: string }
|
|
) {
|
|
return this.queryHandlerService.createQuery(queryData);
|
|
}
|
|
|
|
@Post("update/:id")
|
|
async updateQuery(
|
|
@Body() updateData: Partial<{ source: string }>,
|
|
@Inject("id") id: string
|
|
) {
|
|
return this.queryHandlerService.updateQuery(id, updateData);
|
|
}
|
|
|
|
@Post("/run/:token")
|
|
async runQuery(
|
|
@Param("token") token: string,
|
|
@Body() query: Record<string, any>,
|
|
@Headers() headers: Record<string, any>,
|
|
@Res() res: Response
|
|
) {
|
|
const queryResult = await this.queryExecuterService.runQueryQueued(
|
|
token,
|
|
query,
|
|
headers
|
|
);
|
|
|
|
res.status(queryResult.statusCode);
|
|
|
|
if (queryResult.statusCode === 302 && queryResult.headers["Location"]) {
|
|
res.redirect(queryResult.headers["Location"]);
|
|
return;
|
|
}
|
|
|
|
for (const [key, value] of Object.entries(queryResult.headers)) {
|
|
res.setHeader(key, value);
|
|
}
|
|
|
|
res.send(queryResult.response);
|
|
}
|
|
}
|