44 Controller ,
55 Delete ,
66 Get ,
7+ HttpStatusCodeLiteral ,
78 Post ,
89 Put ,
910 Query ,
@@ -13,10 +14,13 @@ import {
1314 Tags ,
1415 TsoaResponse ,
1516} from 'tsoa' ;
17+ import { TerraformError } from '../interfaces/errors' ;
1618import { StateLockRequest } from '../models/interfaces/StateLockRequest' ;
1719import { GithubService } from '../services/GithubService' ;
1820import { StateService } from '../services/StateService' ;
1921
22+ type HeaderType = { [ key : string ] : string | string [ ] } ;
23+
2024@Route ( '/v1' )
2125@Tags ( 'v1' )
2226export class ControllerV1 extends Controller {
@@ -31,9 +35,21 @@ export class ControllerV1 extends Controller {
3135 }
3236
3337 @Get ( )
34- public async getState ( @Request ( ) request : HttpRequest ) : Promise < any > {
35- const identity = await this . githubService . getIdentity ( request ) ;
36- return this . stateService . getState ( identity ) ;
38+ public async getState (
39+ @Request ( ) request : HttpRequest ,
40+ @Res ( ) res : TsoaResponse < 200 | 400 | 401 | 404 , any > ,
41+ ) : Promise < TsoaResponse < HttpStatusCodeLiteral , any , HeaderType > > {
42+ try {
43+ const identity = await this . githubService . getIdentity ( request ) ;
44+ const state = await this . stateService . getState ( identity ) ;
45+ const response = res ( 200 , state ) ;
46+ return response ;
47+ } catch ( e ) {
48+ if ( e instanceof TerraformError ) {
49+ return e . respond ( res ) ;
50+ }
51+ throw e ;
52+ }
3753 }
3854
3955 @Post ( )
@@ -42,38 +58,59 @@ export class ControllerV1 extends Controller {
4258 @Query ( 'ID' ) id : string ,
4359 // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
4460 @Body ( ) state : any ,
45- @Res ( ) res : TsoaResponse < 200 , void > ,
46- ) : Promise < void > {
47- const stateLockRequest = await this . stateService . getRequest ( id ) ;
48- const identity = await this . githubService . getIdentity ( request , stateLockRequest ) ;
49- await this . stateService . saveState ( identity , id , state ) ;
50- const response = res ( 200 ) ;
51- return response ;
61+ @Res ( ) res : TsoaResponse < 200 | 400 | 401 | 404 | 409 , void > ,
62+ ) : Promise < TsoaResponse < HttpStatusCodeLiteral , void , HeaderType > > {
63+ try {
64+ const stateLockRequest = await this . stateService . getRequest ( id ) ;
65+ const identity = await this . githubService . getIdentity ( request , stateLockRequest ) ;
66+ await this . stateService . saveState ( identity , id , state ) ;
67+ const response = res ( 200 ) ;
68+ return response ;
69+ } catch ( e ) {
70+ if ( e instanceof TerraformError ) {
71+ return e . respond ( res ) ;
72+ }
73+ throw e ;
74+ }
5275 }
5376
5477 @Put ( 'lock' )
5578 public async lockState (
5679 @Request ( ) request : HttpRequest ,
5780 @Body ( ) lockRequest : StateLockRequest ,
58- @Res ( ) res : TsoaResponse < 200 , boolean > ,
59- ) : Promise < boolean > {
60- const stateLockRequest = await this . stateService . saveRequest ( lockRequest ) ;
61- const identity = await this . githubService . getIdentity ( request , stateLockRequest ) ;
62- await this . stateService . lockState ( identity , stateLockRequest ) ;
63- const response = res ( 200 , true ) ;
64- return response ;
81+ @Res ( ) res : TsoaResponse < 200 | 400 | 401 | 404 | 409 , boolean > ,
82+ ) : Promise < TsoaResponse < HttpStatusCodeLiteral , boolean , HeaderType > > {
83+ try {
84+ const stateLockRequest = await this . stateService . saveRequest ( lockRequest ) ;
85+ const identity = await this . githubService . getIdentity ( request , stateLockRequest ) ;
86+ await this . stateService . lockState ( identity , stateLockRequest ) ;
87+ const response = res ( 200 , true ) ;
88+ return response ;
89+ } catch ( e ) {
90+ if ( e instanceof TerraformError ) {
91+ return e . respond ( res ) ;
92+ }
93+ throw e ;
94+ }
6595 }
6696
6797 @Delete ( 'lock' )
6898 public async unlockState (
6999 @Request ( ) request : HttpRequest ,
70100 @Body ( ) lockRequest : StateLockRequest ,
71- @Res ( ) res : TsoaResponse < 200 , boolean > ,
72- ) : Promise < boolean > {
73- const stateLockRequest = await this . stateService . getRequest ( lockRequest . ID ) ;
74- const identity = await this . githubService . getIdentity ( request , stateLockRequest ) ;
75- await this . stateService . unlockState ( identity , stateLockRequest ) ;
76- const response = res ( 200 , true ) ;
77- return response ;
101+ @Res ( ) res : TsoaResponse < 200 | 400 | 401 | 404 | 409 , boolean > ,
102+ ) : Promise < TsoaResponse < HttpStatusCodeLiteral , boolean , HeaderType > > {
103+ try {
104+ const stateLockRequest = await this . stateService . getRequest ( lockRequest . ID ) ;
105+ const identity = await this . githubService . getIdentity ( request , stateLockRequest ) ;
106+ await this . stateService . unlockState ( identity , stateLockRequest ) ;
107+ const response = res ( 200 , true ) ;
108+ return response ;
109+ } catch ( e ) {
110+ if ( e instanceof TerraformError ) {
111+ return e . respond ( res ) ;
112+ }
113+ throw e ;
114+ }
78115 }
79116}
0 commit comments