|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + Get, |
| 5 | + HttpException, |
| 6 | + Param, |
| 7 | + Post, |
| 8 | + Delete, |
| 9 | +} from '@nestjs/common'; |
| 10 | +import { ApiTags } from '@nestjs/swagger'; |
| 11 | +import { ThirdPartyManager } from '@gitroom/nestjs-libraries/3rdparties/thirdparty.manager'; |
| 12 | +import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.request'; |
| 13 | +import { Organization } from '@prisma/client'; |
| 14 | +import { AuthService } from '@gitroom/helpers/auth/auth.service'; |
| 15 | +import { UploadFactory } from '@gitroom/nestjs-libraries/upload/upload.factory'; |
| 16 | +import { MediaService } from '@gitroom/nestjs-libraries/database/prisma/media/media.service'; |
| 17 | + |
| 18 | +@ApiTags('Third Party') |
| 19 | +@Controller('/third-party') |
| 20 | +export class ThirdPartyController { |
| 21 | + private storage = UploadFactory.createStorage(); |
| 22 | + |
| 23 | + constructor( |
| 24 | + private _thirdPartyManager: ThirdPartyManager, |
| 25 | + private _mediaService: MediaService, |
| 26 | + ) {} |
| 27 | + |
| 28 | + @Get('/list') |
| 29 | + async getThirdPartyList() { |
| 30 | + return this._thirdPartyManager.getAllThirdParties(); |
| 31 | + } |
| 32 | + |
| 33 | + @Get('/') |
| 34 | + async getSavedThirdParty(@GetOrgFromRequest() organization: Organization) { |
| 35 | + return Promise.all( |
| 36 | + ( |
| 37 | + await this._thirdPartyManager.getAllThirdPartiesByOrganization( |
| 38 | + organization.id |
| 39 | + ) |
| 40 | + ).map((thirdParty) => { |
| 41 | + const { description, fields, position, title, identifier } = |
| 42 | + this._thirdPartyManager.getThirdPartyByName(thirdParty.identifier); |
| 43 | + return { |
| 44 | + ...thirdParty, |
| 45 | + title, |
| 46 | + position, |
| 47 | + fields, |
| 48 | + description, |
| 49 | + }; |
| 50 | + }) |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + @Delete('/:id') |
| 55 | + deleteById( |
| 56 | + @GetOrgFromRequest() organization: Organization, |
| 57 | + @Param('id') id: string |
| 58 | + ) { |
| 59 | + return this._thirdPartyManager.deleteIntegration(organization.id, id); |
| 60 | + } |
| 61 | + |
| 62 | + @Post('/:id/submit') |
| 63 | + async generate( |
| 64 | + @GetOrgFromRequest() organization: Organization, |
| 65 | + @Param('id') id: string, |
| 66 | + @Body() data: any |
| 67 | + ) { |
| 68 | + const thirdParty = await this._thirdPartyManager.getIntegrationById( |
| 69 | + organization.id, |
| 70 | + id |
| 71 | + ); |
| 72 | + |
| 73 | + if (!thirdParty) { |
| 74 | + throw new HttpException('Integration not found', 404); |
| 75 | + } |
| 76 | + |
| 77 | + const thirdPartyInstance = this._thirdPartyManager.getThirdPartyByName( |
| 78 | + thirdParty.identifier |
| 79 | + ); |
| 80 | + |
| 81 | + if (!thirdPartyInstance) { |
| 82 | + throw new HttpException('Invalid identifier', 400); |
| 83 | + } |
| 84 | + |
| 85 | + const loadedData = await thirdPartyInstance?.instance?.sendData( |
| 86 | + AuthService.fixedDecryption(thirdParty.apiKey), |
| 87 | + data |
| 88 | + ); |
| 89 | + |
| 90 | + const file = await this.storage.uploadSimple(loadedData); |
| 91 | + return this._mediaService.saveFile(organization.id, file.split('/').pop(), file); |
| 92 | + } |
| 93 | + |
| 94 | + @Post('/function/:id/:functionName') |
| 95 | + async callFunction( |
| 96 | + @GetOrgFromRequest() organization: Organization, |
| 97 | + @Param('id') id: string, |
| 98 | + @Param('functionName') functionName: string, |
| 99 | + @Body() data: any |
| 100 | + ) { |
| 101 | + const thirdParty = await this._thirdPartyManager.getIntegrationById( |
| 102 | + organization.id, |
| 103 | + id |
| 104 | + ); |
| 105 | + |
| 106 | + if (!thirdParty) { |
| 107 | + throw new HttpException('Integration not found', 404); |
| 108 | + } |
| 109 | + |
| 110 | + const thirdPartyInstance = this._thirdPartyManager.getThirdPartyByName( |
| 111 | + thirdParty.identifier |
| 112 | + ); |
| 113 | + |
| 114 | + if (!thirdPartyInstance) { |
| 115 | + throw new HttpException('Invalid identifier', 400); |
| 116 | + } |
| 117 | + |
| 118 | + return thirdPartyInstance?.instance?.[functionName]( |
| 119 | + AuthService.fixedDecryption(thirdParty.apiKey), |
| 120 | + data |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + @Post('/:identifier') |
| 125 | + async addApiKey( |
| 126 | + @GetOrgFromRequest() organization: Organization, |
| 127 | + @Param('identifier') identifier: string, |
| 128 | + @Body('api') api: string |
| 129 | + ) { |
| 130 | + const thirdParty = this._thirdPartyManager.getThirdPartyByName(identifier); |
| 131 | + if (!thirdParty) { |
| 132 | + throw new HttpException('Invalid identifier', 400); |
| 133 | + } |
| 134 | + |
| 135 | + const connect = await thirdParty.instance.checkConnection(api); |
| 136 | + if (!connect) { |
| 137 | + throw new HttpException('Invalid API key', 400); |
| 138 | + } |
| 139 | + |
| 140 | + try { |
| 141 | + const save = await this._thirdPartyManager.saveIntegration( |
| 142 | + organization.id, |
| 143 | + identifier, |
| 144 | + api, |
| 145 | + { |
| 146 | + name: connect.name, |
| 147 | + username: connect.username, |
| 148 | + id: connect.id, |
| 149 | + } |
| 150 | + ); |
| 151 | + |
| 152 | + return { |
| 153 | + id: save.id, |
| 154 | + }; |
| 155 | + } catch (e) { |
| 156 | + console.log(e); |
| 157 | + throw new HttpException('Integration Already Exists', 400); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments