org.controller.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { Body, Controller, ForbiddenException, Get, Param, Post, Put, Req, Res, Sse, Headers } from '@nestjs/common'
  2. import { Org } from './entities/org.entity'
  3. import { PageRequest } from '../common/dto/page-request'
  4. import { OrgService } from './org.service'
  5. import { HasRoles } from '../auth/roles.decorator'
  6. import { Role } from '../model/role.enum'
  7. import { Public } from 'src/auth/public.decorator'
  8. @Controller('org')
  9. export class OrgController {
  10. constructor(private readonly orgService: OrgService) {}
  11. @Get()
  12. @Public()
  13. async getOrgFromUrl(@Headers('host') host) {
  14. return await this.orgService.findByUrl(host.replace(/^http(s?):\/\//, '').replace(/\/$/, ''))
  15. }
  16. @Get('/my')
  17. async my(@Req() req) {
  18. if (!req.user.orgId) {
  19. throw new ForbiddenException('You are not a member of any organization')
  20. }
  21. return await this.orgService.findById(req.user.orgId)
  22. }
  23. @Get('/:id')
  24. @Public()
  25. async get(@Param('id') id: string) {
  26. return await this.orgService.findById(Number(id))
  27. }
  28. @Put('/my')
  29. async update(@Req() req, @Body() org: Org) {
  30. if (!req.user.orgId) {
  31. throw new ForbiddenException('You are not a member of any organization')
  32. }
  33. org.id = req.user.orgId
  34. return await this.orgService.update(org)
  35. }
  36. @Post('/:id/ask')
  37. async ask(
  38. @Req() req,
  39. @Param('id') id: string,
  40. @Body()
  41. body: {
  42. prompt: string
  43. options: {
  44. parentMessageId?: string
  45. }
  46. knowledgeId?: number
  47. fileId?: number
  48. }
  49. ) {
  50. const orgId = Number(id)
  51. // if (req.user.orgId != orgId) {
  52. // throw new ForbiddenException('You are not a member of this organization')
  53. // }
  54. return await this.orgService.ask(body.prompt, orgId, body.options?.parentMessageId, body.knowledgeId, body.fileId)
  55. }
  56. @Post('/:id/streamAsk')
  57. @Sse()
  58. async streamAsk(@Req() req, @Res() res, @Param('id') id: string, @Body() body: { prompt: string }) {
  59. const orgId = Number(id)
  60. // if (req.user.orgId != orgId) {
  61. // throw new ForbiddenException('You are not a member of this organization')
  62. // }
  63. await this.orgService.streamAsk(req, res, body.prompt, orgId)
  64. }
  65. }