-
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.
Merge pull request #7 from TrustlyInc/DEV
Update main branch
- Loading branch information
Showing
13 changed files
with
198 additions
and
45 deletions.
There are no files selected for viewing
File renamed without changes.
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,30 @@ | ||
### Description | ||
|
||
_(Insert pull request description)_ | ||
|
||
--- | ||
|
||
### Relevant Commits | ||
|
||
_(List of main updates)_ | ||
|
||
--- | ||
|
||
### Requirements | ||
|
||
_(Please check if your pull request fulfills the following requirements)_ | ||
|
||
- [ ] Changes were properly tested (attach evidence if applicable) | ||
- [ ] Repository's code-style/linting compliant | ||
|
||
--- | ||
|
||
### Evidence | ||
|
||
_(Insert any evidence related to this pull request. Leave it as **"None"** or **"Not applicable"** if you have nothing to add)_ | ||
|
||
--- | ||
|
||
### Additional Information | ||
|
||
_(Insert any additional information related to this pull request, as more context or Jira ticket. Leave it as **"None"** or **"Not applicable"** if you have nothing to add)_ |
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 |
---|---|---|
|
@@ -2,6 +2,9 @@ | |
/dist | ||
/node_modules | ||
|
||
# environment variables file | ||
.env | ||
|
||
# Logs | ||
logs | ||
*.log | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,48 @@ | ||
import { Body, Controller, Get, Post, RawBodyRequest, Req } from '@nestjs/common'; | ||
import { | ||
Body, | ||
Controller, | ||
Post, | ||
RawBodyRequest, | ||
Req, | ||
Res, | ||
} from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { ApiParam, ApiCreatedResponse} from '@nestjs/swagger' | ||
import { ApiParam, ApiCreatedResponse } from '@nestjs/swagger'; | ||
import { RequestSignatureService } from './request-signature.service'; | ||
import { normalizeEstablishData } from './request-signature.utils'; | ||
import { EstablishDto } from '../Dtos/establish.dto'; | ||
import { CustomerDto } from 'src/Dtos/customer.dto'; | ||
import { AddressDto } from 'src/Dtos/address.dto'; | ||
import { Response } from 'express'; | ||
|
||
@Controller() | ||
export class RequestSignatureController { | ||
constructor(private readonly requestSignatureService: RequestSignatureService, private readonly configService: ConfigService) {} | ||
constructor( | ||
private readonly requestSignatureService: RequestSignatureService, | ||
private readonly configService: ConfigService | ||
) {} | ||
|
||
ACCESS_KEY = this.configService.get<String>('ACCESS_KEY') | ||
ACCESS_KEY = this.configService.get<String>('ACCESS_KEY'); | ||
|
||
@Post('/signature') | ||
@ApiParam({name: 'establish', required: true, schema: new EstablishDto() }) | ||
@ApiParam({ name: 'establish', required: true, schema: new EstablishDto() }) | ||
@ApiCreatedResponse({ | ||
type: String, | ||
}) | ||
createRequestSignature(@Body() establish: EstablishDto, @Req() req: RawBodyRequest<Request>): string { | ||
let rawBody = req.rawBody; | ||
createRequestSignature( | ||
@Body() establish: EstablishDto, | ||
@Req() req: RawBodyRequest<Request>, | ||
@Res() res: Response | ||
): object { | ||
const rawBody = req.rawBody; | ||
|
||
if (rawBody){ | ||
let json = JSON.parse(rawBody.toString()); | ||
if (rawBody) { | ||
establish = normalizeEstablishData(establish, rawBody); | ||
} | ||
|
||
if(json['customer.name']){ | ||
let address = new AddressDto(); | ||
address.country = json['customer.address.country']; | ||
|
||
let customer = new CustomerDto(); | ||
customer.name = json['customer.name']; | ||
customer.address = address; | ||
|
||
establish.customer = customer; | ||
} | ||
|
||
} | ||
|
||
return this.requestSignatureService.getRequestSignature(establish, this.ACCESS_KEY as string); | ||
return res.json( | ||
this.requestSignatureService.getRequestSignature( | ||
establish, | ||
this.ACCESS_KEY as 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,21 @@ | ||
import { EstablishDto } from '../Dtos/establish.dto'; | ||
import { normalizeEstablishData } from './request-signature.utils'; | ||
|
||
describe('normalizeEstablishData', () => { | ||
test('converts dot notation into nested object', () => { | ||
const dotNotationExample = { 'customer.address.country': 'US' }; | ||
const nestedObjectExpectation = { | ||
customer: { address: { country: 'US' } }, | ||
}; | ||
|
||
const establish = { | ||
...new EstablishDto(), | ||
...dotNotationExample, | ||
}; | ||
const rawBody = Buffer.from(JSON.stringify(dotNotationExample), 'utf-8'); | ||
|
||
const result = normalizeEstablishData(establish, rawBody); | ||
|
||
expect(result).toEqual(nestedObjectExpectation); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { HttpModule } from "@nestjs/axios"; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import { TransactionService } from './transaction.service'; | ||
import { TransactionController } from './transaction.controller'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
envFilePath: '.development.env' | ||
}), | ||
HttpModule, | ||
], | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
envFilePath: '.env', | ||
}), | ||
HttpModule, | ||
], | ||
controllers: [TransactionController], | ||
providers: [TransactionService], | ||
}) | ||
|
||
export class TransactionModule {} | ||
|
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,41 @@ | ||
import { convertDotNotationIntoNestedObject } from './normalize'; | ||
|
||
describe('convertDotNotationIntoNestedObject', () => { | ||
test('converts dot notation into nested object', () => { | ||
const input = { | ||
'customer.address.country': 'US', | ||
'customer.email': '[email protected]', | ||
'customer.name': 'John', | ||
}; | ||
|
||
const expectedOutput = { | ||
customer: { | ||
address: { | ||
country: 'US', | ||
}, | ||
email: '[email protected]', | ||
name: 'John', | ||
}, | ||
}; | ||
|
||
const result = convertDotNotationIntoNestedObject(input); | ||
|
||
expect(result).toEqual(expectedOutput); | ||
}); | ||
|
||
test('returns the same object reference for object literal input', () => { | ||
const input = { | ||
customer: { | ||
address: { | ||
country: 'US', | ||
}, | ||
email: '[email protected]', | ||
name: 'John', | ||
}, | ||
}; | ||
|
||
const result = convertDotNotationIntoNestedObject(input); | ||
|
||
expect(result).toEqual(input); | ||
}); | ||
}); |
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,22 @@ | ||
type NestedObject = { [key: string]: NestedObject | string }; | ||
|
||
export function convertDotNotationIntoNestedObject( | ||
obj: NestedObject | ||
): NestedObject { | ||
const result: NestedObject = {}; | ||
|
||
for (const key in obj) { | ||
const parts = key.split('.'); | ||
let current: NestedObject = result; | ||
|
||
for (let i = 0; i < parts.length - 1; i++) { | ||
const part = parts[i]; | ||
current[part] = current[part] || {}; | ||
current = current[part] as NestedObject; | ||
} | ||
|
||
current[parts[parts.length - 1]] = obj[key]; | ||
} | ||
|
||
return result; | ||
} |