@@ -13,17 +13,17 @@ import {
1313 Req ,
1414 UseGuards ,
1515} from "@nestjs/common" ;
16- import { UnauthorizedException } from "@nestjs/common" ;
1716import {
1817 ApiBadRequestResponse ,
1918 ApiBearerAuth ,
2019 ApiForbiddenResponse ,
20+ ApiNotFoundResponse ,
2121 ApiOperation ,
2222 ApiTags ,
2323 ApiUnauthorizedResponse ,
2424} from "@nestjs/swagger" ;
2525
26- import { ComposeAuthGuard } from ' @auth/compose-auth.guard' ;
26+ import { ComposeAuthGuard } from " @auth/compose-auth.guard" ;
2727import { Read , ApplicationAdmin } from "@auth/roles.decorator" ;
2828import { RolesGuard } from "@auth/roles.guard" ;
2929import { CreateDataTargetDto } from "@dto/create-data-target.dto" ;
@@ -41,6 +41,8 @@ import {
4141import { DataTargetService } from "@services/data-targets/data-target.service" ;
4242import { AuditLog } from "@services/audit-log.service" ;
4343import { ActionType } from "@entities/audit-log-entry" ;
44+ import { OrganizationService } from "@services/user-management/organization.service" ;
45+ import { OddkMailInfo } from "@dto/oddk-mail-info.dto" ;
4446
4547@ApiTags ( "Data Target" )
4648@Controller ( "data-target" )
@@ -50,7 +52,10 @@ import { ActionType } from "@entities/audit-log-entry";
5052@ApiForbiddenResponse ( )
5153@ApiUnauthorizedResponse ( )
5254export class DataTargetController {
53- constructor ( private dataTargetService : DataTargetService ) { }
55+ constructor (
56+ private dataTargetService : DataTargetService ,
57+ private organizationService : OrganizationService
58+ ) { }
5459
5560 @Get ( )
5661 @ApiOperation ( { summary : "Find all DataTargets" } )
@@ -65,7 +70,11 @@ export class DataTargetController {
6570 query . applicationId = + query . applicationId ;
6671 }
6772
68- checkIfUserHasAccessToApplication ( req , query . applicationId , ApplicationAccessScope . Read ) ;
73+ checkIfUserHasAccessToApplication (
74+ req ,
75+ query . applicationId ,
76+ ApplicationAccessScope . Read
77+ ) ;
6978 const allowed = req . user . permissions . getAllApplicationsWithAtLeastRead ( ) ;
7079
7180 return await this . dataTargetService . findAndCountAllWithPagination (
@@ -83,7 +92,11 @@ export class DataTargetController {
8392 ) : Promise < DataTarget > {
8493 try {
8594 const dataTarget = await this . dataTargetService . findOne ( id ) ;
86- checkIfUserHasAccessToApplication ( req , dataTarget . application . id , ApplicationAccessScope . Read ) ;
95+ checkIfUserHasAccessToApplication (
96+ req ,
97+ dataTarget . application . id ,
98+ ApplicationAccessScope . Read
99+ ) ;
87100 return dataTarget ;
88101 } catch ( err ) {
89102 throw new NotFoundException ( ErrorCodes . IdDoesNotExists ) ;
@@ -132,9 +145,17 @@ export class DataTargetController {
132145 ) : Promise < DataTarget > {
133146 const oldDataTarget = await this . dataTargetService . findOne ( id ) ;
134147 try {
135- checkIfUserHasAccessToApplication ( req , oldDataTarget . application . id , ApplicationAccessScope . Write ) ;
148+ checkIfUserHasAccessToApplication (
149+ req ,
150+ oldDataTarget . application . id ,
151+ ApplicationAccessScope . Write
152+ ) ;
136153 if ( oldDataTarget . application . id !== updateDto . applicationId ) {
137- checkIfUserHasAccessToApplication ( req , updateDto . applicationId , ApplicationAccessScope . Write ) ;
154+ checkIfUserHasAccessToApplication (
155+ req ,
156+ updateDto . applicationId ,
157+ ApplicationAccessScope . Write
158+ ) ;
138159 }
139160 } catch ( err ) {
140161 AuditLog . fail (
@@ -172,7 +193,11 @@ export class DataTargetController {
172193 ) : Promise < DeleteResponseDto > {
173194 try {
174195 const dt = await this . dataTargetService . findOne ( id ) ;
175- checkIfUserHasAccessToApplication ( req , dt . application . id , ApplicationAccessScope . Write ) ;
196+ checkIfUserHasAccessToApplication (
197+ req ,
198+ dt . application . id ,
199+ ApplicationAccessScope . Write
200+ ) ;
176201 const result = await this . dataTargetService . delete ( id ) ;
177202
178203 if ( result . affected === 0 ) {
@@ -188,4 +213,47 @@ export class DataTargetController {
188213 throw new NotFoundException ( err ) ;
189214 }
190215 }
216+
217+ @Get ( "getOpenDataDkRegistered/:organizationId" )
218+ @ApiOperation ( { summary : "Get OpenDataDkRegistered-status for given OrganizationId" } )
219+ @ApiNotFoundResponse ( )
220+ @Read ( )
221+ async getOpenDataDkRegistered ( @Param ( "organizationId" , new ParseIntPipe ( ) ) organizationId : number ) : Promise < boolean > {
222+ try {
223+ return ( await this . organizationService . findById ( organizationId ) ) ?. openDataDkRegistered ;
224+ } catch ( err ) {
225+ throw new NotFoundException ( ErrorCodes . IdDoesNotExists ) ;
226+ }
227+ }
228+
229+ @Put ( "updateOpenDataDkRegistered/:organizationId" )
230+ @ApiOperation ( {
231+ summary : "Update the OpenDataDkRegistered to true, for the given OrganizationId - to stop showing the dialog for sending ODDK-mail on creation of new datatargets" ,
232+ } )
233+ @ApiNotFoundResponse ( )
234+ async updateOpenDataDkRegistered (
235+ @Req ( ) req : AuthenticatedRequest ,
236+ @Param ( "organizationId" , new ParseIntPipe ( ) ) organizationId : number
237+ ) : Promise < boolean > {
238+ try {
239+ await this . organizationService . updateOpenDataDkRegistered ( organizationId , req . user . userId ) ;
240+ return true ;
241+ } catch ( err ) {
242+ if ( err . name == "EntityNotFound" ) {
243+ throw new NotFoundException ( ) ;
244+ }
245+ throw err ;
246+ }
247+ }
248+
249+ @Post ( "sendOpenDataDkMail" )
250+ @ApiOperation ( { summary : "Send mail for registering datatargets to Open Data DK, for the given OrganizationId" } )
251+ async sendOpenDataDkMail (
252+ @Req ( ) req : AuthenticatedRequest ,
253+ @Body ( ) mailInfoDto : OddkMailInfo
254+ ) : Promise < boolean > {
255+ await this . dataTargetService . sendOpenDataDkMail ( mailInfoDto , req . user . userId ) ;
256+ await this . organizationService . updateOpenDataDkRegistered ( mailInfoDto . organizationId , req . user . userId ) ;
257+ return true ;
258+ }
191259}
0 commit comments