-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from IndustryFusion/feature/task-004-03-add_co…
…pyright added region_code fetch
- Loading branch information
Showing
13 changed files
with
359 additions
and
97 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
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
20 changes: 20 additions & 0 deletions
20
backend/src/endpoints/contract/contract.controller.spec.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,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { ContractController } from './contract.controller'; | ||
import { ContractService } from './contract.service'; | ||
|
||
describe('ContractController', () => { | ||
let controller: ContractController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [ContractController], | ||
providers: [ContractService], | ||
}).compile(); | ||
|
||
controller = module.get<ContractController>(ContractController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
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, Post, Body, Patch, Param, Delete } from '@nestjs/common'; | ||
import { ContractService } from './contract.service'; | ||
import { CreateContractDto, CreateBindingDto } from './dto/create-contract.dto'; | ||
|
||
@Controller('contract') | ||
export class ContractController { | ||
constructor(private readonly contractService: ContractService) {} | ||
|
||
@Post('/binding') | ||
createBinding(@Body() createBindingDto: CreateBindingDto) { | ||
return this.contractService.createBinding(createBindingDto); | ||
} | ||
|
||
@Post() | ||
createContract(@Body() createContractDto: CreateContractDto) { | ||
return this.contractService.createContract(createContractDto); | ||
} | ||
} |
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,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ContractService } from './contract.service'; | ||
import { ContractController } from './contract.controller'; | ||
|
||
@Module({ | ||
controllers: [ContractController], | ||
providers: [ContractService], | ||
}) | ||
export class ContractModule {} |
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 { Test, TestingModule } from '@nestjs/testing'; | ||
import { ContractService } from './contract.service'; | ||
|
||
describe('ContractService', () => { | ||
let service: ContractService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ContractService], | ||
}).compile(); | ||
|
||
service = module.get<ContractService>(ContractService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,78 @@ | ||
import { Injectable, InternalServerErrorException } from '@nestjs/common'; | ||
import { CreateContractDto, CreateBindingDto } from './dto/create-contract.dto'; | ||
import { Urn } from 'src/schemas/urn.schema'; | ||
import { InjectModel } from '@nestjs/mongoose'; | ||
import { Model } from 'mongoose'; | ||
import { v5 as uuidv5, validate as uuidValidate } from 'uuid'; | ||
import * as moment from 'moment'; | ||
import { Contract } from 'src/schemas/contract.schema'; | ||
import { Binding } from 'src/schemas/binding.schema'; | ||
|
||
@Injectable() | ||
export class ContractService { | ||
constructor( | ||
@InjectModel(Urn.name) | ||
private urnModel: Model<Urn>, | ||
@InjectModel(Contract.name) | ||
private contractModel: Model<Contract>, | ||
@InjectModel(Binding.name) | ||
private bindingModel: Model<Binding> | ||
) {} | ||
private readonly ifricId = process.env.IFRIC_NAMESPACE; | ||
private readonly contractCode = process.env.CONTRACT_DEFAULT_CODE; | ||
private readonly bindingCode = process.env.BINDING_DEFAULT_CODE; | ||
|
||
async createBinding(data: CreateBindingDto) { | ||
try { | ||
let uuid = uuidv5(data.data_provider_company_ifric_id, this.ifricId); | ||
let bindingCodeArr = this.bindingCode.split('-'); | ||
let region_code = data.data_provider_company_ifric_id.split("-")[1]; | ||
let ifricId = `urn:ifric:${bindingCodeArr[0].toLowerCase()}-${region_code}-${bindingCodeArr[1].toLowerCase()}-${bindingCodeArr[2].toLowerCase()}-${uuid}`; | ||
const urnData = new this.urnModel({ | ||
urn: ifricId, | ||
created_at: moment().format(), | ||
last_updated_at: moment().format() | ||
}) | ||
const savedUrnData = await urnData.save(); | ||
|
||
const bindingData = new this.bindingModel({ | ||
urn_id: savedUrnData._id, | ||
binding_datetime_string: data.binding_datetime_string, | ||
data_provider_company_ifric_id: data.data_provider_company_ifric_id, | ||
data_consumer_company_ifric_id: data.data_consumer_company_ifric_id, | ||
created_at: moment().format(), | ||
last_updated_at: moment().format() | ||
}) | ||
await bindingData.save(); | ||
return { status: 201, message: 'Binding created successfully', urn_id: ifricId }; | ||
} catch(err) { | ||
throw new InternalServerErrorException(`Failed to create binding: ${err.message}`); | ||
} | ||
} | ||
|
||
async createContract(data: CreateContractDto) { | ||
try { | ||
let uuid = uuidv5(data.data_consumer_company_ifric_id, this.ifricId); | ||
let contractCodeArr = this.contractCode.split('-'); | ||
let region_code = data.data_consumer_company_ifric_id.split("-")[1]; | ||
let ifricId = `urn:ifric:${contractCodeArr[0].toLowerCase()}-${region_code}-${contractCodeArr[1].toLowerCase()}-${contractCodeArr[2].toLowerCase()}-${uuid}`; | ||
const urnData = new this.urnModel({ | ||
urn: ifricId, | ||
created_at: moment().format(), | ||
last_updated_at: moment().format() | ||
}) | ||
const savedUrnData = await urnData.save(); | ||
const contractData = new this.contractModel({ | ||
urn_id: savedUrnData._id, | ||
contract_datetime_string: data.contract_datetime_string, | ||
data_consumer_company_ifric_id: data.data_consumer_company_ifric_id, | ||
created_at: moment().format(), | ||
last_updated_at: moment().format() | ||
}) | ||
await contractData.save(); | ||
return { status: 201, message: 'Contract created successfully', urn_id: ifricId }; | ||
} catch(err) { | ||
throw new InternalServerErrorException(`Failed to create contract: ${err.message}`); | ||
} | ||
} | ||
} |
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,10 @@ | ||
export class CreateContractDto { | ||
data_consumer_company_ifric_id: string; | ||
contract_datetime_string: string; | ||
} | ||
|
||
export class CreateBindingDto { | ||
data_consumer_company_ifric_id: string; | ||
data_provider_company_ifric_id: string; | ||
binding_datetime_string: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class Contract {} |
Oops, something went wrong.