-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
289 additions
and
5 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
69 changes: 69 additions & 0 deletions
69
packages/api/src/@core/catalog-options/catalog-options.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,69 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Get, | ||
Post, | ||
Query, | ||
UseGuards, | ||
Request, | ||
} from '@nestjs/common'; | ||
import { LoggerService } from '../logger/logger.service'; | ||
import { | ||
ApiBody, | ||
ApiOperation, | ||
ApiQuery, | ||
ApiResponse, | ||
ApiTags, | ||
} from '@nestjs/swagger'; | ||
import { CatalogOptionsService } from './catalog-options.service'; | ||
import { CreateCatalogOptionsDto } from './dto/catalog-options.dto'; | ||
import { JwtAuthGuard } from '@@core/auth/guards/jwt-auth.guard'; | ||
|
||
|
||
@ApiTags('catalog-options') | ||
@Controller('catalog-options') | ||
export class CatalogOptionsController { | ||
constructor( | ||
private readonly catalogOptionsService: CatalogOptionsService, | ||
private logger: LoggerService, | ||
) { | ||
this.logger.setContext(CatalogOptionsController.name); | ||
} | ||
|
||
@ApiOperation({ operationId: 'CreateCatalogOptions', summary: 'Add or Update Catalog' }) | ||
@ApiBody({ type: CreateCatalogOptionsDto }) | ||
@ApiResponse({ status: 201 }) | ||
@UseGuards(JwtAuthGuard) | ||
@Post() | ||
addCatalogOptions(@Body() catalogOptionsDto: CreateCatalogOptionsDto) { | ||
return this.catalogOptionsService.createCatalogOptions(catalogOptionsDto); | ||
} | ||
|
||
|
||
// It should be public API and don't have to add AuthGuard | ||
@ApiOperation({ | ||
operationId: 'getCatalog', | ||
summary: 'Retrieve a Catalog Options by Project ID', | ||
}) | ||
@ApiQuery({ name: 'projectID', required: true, type: String }) | ||
@ApiResponse({ status: 200 }) | ||
@Get('single') | ||
getLinkedUser(@Query('projectID') id: string) { | ||
// validate project_id against user | ||
return this.catalogOptionsService.getCatalogByProjectId(id); | ||
} | ||
|
||
@ApiOperation({ | ||
operationId: 'getCatalog', | ||
summary: 'Retrieve a Catalog Options by User ID', | ||
}) | ||
@ApiQuery({ name: 'userID', required: true, type: String }) | ||
@ApiResponse({ status: 200 }) | ||
@UseGuards(JwtAuthGuard) | ||
@Get('single') | ||
getLinkedUserV2(@Query('userID') id: string) { | ||
// validate project_id against user | ||
return this.catalogOptionsService.getCatalogByUserId(id); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
packages/api/src/@core/catalog-options/catalog-options.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,11 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CatalogOptionsService } from './catalog-options.service'; | ||
import { CatalogOptionsController } from './catalog-options.controller'; | ||
import { LoggerService } from '../logger/logger.service'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
|
||
@Module({ | ||
providers: [CatalogOptionsService, LoggerService, PrismaService], | ||
controllers: [CatalogOptionsController], | ||
}) | ||
export class CatalogOptionsModule { } |
107 changes: 107 additions & 0 deletions
107
packages/api/src/@core/catalog-options/catalog-options.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,107 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { LoggerService } from '../logger/logger.service'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { handleServiceError } from '@@core/utils/errors'; | ||
import { CreateCatalogOptionsDto } from './dto/catalog-options.dto'; | ||
|
||
@Injectable() | ||
export class CatalogOptionsService { | ||
constructor(private prisma: PrismaService, private logger: LoggerService) { | ||
this.logger.setContext(CatalogOptionsService.name); | ||
} | ||
|
||
async createCatalogOptions(data: CreateCatalogOptionsDto) { | ||
try { | ||
const { id_user, selected_catalog } = data; | ||
const existingCatalogOptions = await this.prisma.catalog_options.findFirst({ | ||
where: { | ||
id_user | ||
} | ||
}); | ||
|
||
if (!existingCatalogOptions) { | ||
const res = await this.prisma.catalog_options.create({ | ||
data: { | ||
id_catalog_option: uuidv4(), | ||
selected_catalog: selected_catalog, | ||
id_user: id_user | ||
} | ||
}); | ||
|
||
return res; | ||
} | ||
const res = await this.prisma.catalog_options.update({ | ||
where: { | ||
id_catalog_option: existingCatalogOptions.id_catalog_option | ||
}, | ||
data: { | ||
selected_catalog: selected_catalog | ||
} | ||
}); | ||
|
||
return res; | ||
|
||
} catch (error) { | ||
handleServiceError(error, this.logger); | ||
} | ||
} | ||
|
||
async getCatalogByProjectId(id_project: string) { | ||
try { | ||
const user = await this.prisma.projects.findFirst({ | ||
where: { | ||
id_project | ||
}, | ||
select: { | ||
id_user: true | ||
} | ||
}); | ||
|
||
if (!user) { | ||
throw new NotFoundException("Project Id does not exist!") | ||
} | ||
|
||
const res = await this.prisma.catalog_options.findFirst({ | ||
where: { | ||
id_user: user.id_user | ||
} | ||
}); | ||
if (!res) { | ||
throw new NotFoundException("Catalog Options not found for current User!") | ||
} | ||
|
||
return res | ||
|
||
|
||
} catch (error) { | ||
handleServiceError(error, this.logger); | ||
} | ||
} | ||
|
||
|
||
async getCatalogByUserId(id_user: string) { | ||
|
||
try { | ||
|
||
const res = await this.prisma.catalog_options.findFirst({ | ||
where: { | ||
id_user: id_user | ||
} | ||
}); | ||
if (!res) { | ||
throw new NotFoundException("Catalog Options not found for current User!") | ||
} | ||
|
||
return res; | ||
|
||
|
||
} catch (error) { | ||
handleServiceError(error, this.logger); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
} |
8 changes: 8 additions & 0 deletions
8
packages/api/src/@core/catalog-options/dto/catalog-options.dto.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,8 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class CreateCatalogOptionsDto { | ||
@ApiProperty() | ||
id_user: string; | ||
@ApiProperty() | ||
selected_catalog: string; | ||
} |
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