-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): add base setup for external integrations
- Loading branch information
1 parent
cd09b40
commit 06eb566
Showing
12 changed files
with
164 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/api/src/modules/database/migrations/1627855502271-ExternalAutomationToken.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class ExternalAutomationToken1627855502271 implements MigrationInterface { | ||
name = 'ExternalAutomationToken1627855502271'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "users" ADD "externalAutomationToken" character varying`); | ||
await queryRunner.query( | ||
`ALTER TABLE "users" ADD CONSTRAINT "UQ_bb0586787a0de49b8a4edd82f3c" UNIQUE ("externalAutomationToken")`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "users" DROP CONSTRAINT "UQ_bb0586787a0de49b8a4edd82f3c"`); | ||
await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "externalAutomationToken"`); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/api/src/modules/external-automations/external-automations.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Controller, Get, Headers } from '@nestjs/common'; | ||
|
||
import { ExternalAutomationsService } from './external-automations.service'; | ||
|
||
interface ToggleGateHeaders { | ||
from: string; | ||
authorization: string; | ||
} | ||
|
||
@Controller('external-automations') | ||
export class ExternalAutomationsController { | ||
constructor(private readonly externalAutomationsService: ExternalAutomationsService) {} | ||
|
||
@Get('toggle-gate') | ||
toggleGate(@Headers() { authorization, from }: ToggleGateHeaders) { | ||
return this.externalAutomationsService.toggleGate(authorization, from); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/api/src/modules/external-automations/external-automations.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { UsersModule } from '../users/users.module'; | ||
import { WebsocketModule } from '../websocket/websocket.module'; | ||
import { ExternalAutomationsController } from './external-automations.controller'; | ||
import { ExternalAutomationsService } from './external-automations.service'; | ||
|
||
@Module({ | ||
controllers: [ExternalAutomationsController], | ||
providers: [ExternalAutomationsService], | ||
imports: [WebsocketModule, UsersModule], | ||
}) | ||
export class ExternalAutomationsModule {} |
43 changes: 43 additions & 0 deletions
43
packages/api/src/modules/external-automations/external-automations.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Injectable, Logger, UnauthorizedException } from '@nestjs/common'; | ||
import { v4 as uuidV4 } from 'uuid'; | ||
|
||
import { UsersService } from '../users/users.service'; | ||
import { Websocket } from '../websocket/websocket.gateway'; | ||
|
||
@Injectable() | ||
export class ExternalAutomationsService { | ||
constructor( | ||
private readonly websocketGateway: Websocket, | ||
private readonly usersService: UsersService, | ||
) {} | ||
|
||
private logger: Logger = new Logger(ExternalAutomationsService.name); | ||
|
||
async generateToken(email: string): Promise<string> { | ||
const token = uuidV4(); | ||
|
||
const { externalAutomationToken } = await this.usersService.updateExternalAutomationToken( | ||
email, | ||
token, | ||
); | ||
this.logger.log(`New external automation token generated for ${email}`); | ||
|
||
return externalAutomationToken; | ||
} | ||
|
||
private async validateToken(token: string, email: string): Promise<true> { | ||
const foundUser = await this.usersService.findOneByEmail(email); | ||
|
||
if (!foundUser || token !== foundUser.externalAutomationToken) { | ||
throw new UnauthorizedException(); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
async toggleGate(token: string, email: string): Promise<string> { | ||
await this.validateToken(token, email); | ||
// this.websocketGateway.toggleGate(); | ||
return 'Successfully open'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/client/src/pages/authorized/Settings/sections/Account/tabs/AutomationsTab.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import Link from '../../../../../../elements/Link'; | ||
import { KeyIcon } from '../../../../../../icons'; | ||
import { StyledButton } from '../Account.styled'; | ||
|
||
const AutomationsTab = () => { | ||
const { t } = useTranslation(); | ||
|
||
return ( | ||
<div> | ||
<Link to="https://www.icloud.com/shortcuts/5f754527e564467d8b56322d2fab010b" asOuterLink> | ||
Apple shortcut EN | ||
</Link> | ||
<br /> | ||
<Link to="https://www.icloud.com/shortcuts/2f72b75a096549bc8352b317f7d7c9fc" asOuterLink> | ||
Apple shortcut PL | ||
</Link> | ||
<StyledButton type="submit" fullWidth> | ||
{t('routes.settings.account.automations.generateApiKey')} | ||
<KeyIcon /> | ||
</StyledButton> | ||
</div> | ||
); | ||
}; | ||
|
||
export default AutomationsTab; |