-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
3d135cc
commit 1bdf7f7
Showing
38 changed files
with
1,325 additions
and
27 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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export interface SendInput { | ||
from: string; | ||
to: string; | ||
title: string; | ||
body: string; | ||
} | ||
|
||
export interface EmailAdapter { | ||
send: (i: SendInput) => Promise<void>; | ||
} |
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,19 @@ | ||
import { Readable } from 'stream'; | ||
|
||
export interface SaveInput { | ||
folder: string; | ||
filePath: `/${string}`; | ||
file: Buffer; | ||
metadata?: Record<string, string>; | ||
} | ||
|
||
export interface GetInput { | ||
folder: string; | ||
filePath: `/${string}`; | ||
} | ||
|
||
export interface FileAdapter { | ||
save: (i: SaveInput) => Promise<string>; | ||
|
||
getReadStream: (i: GetInput) => Promise<Readable>; | ||
} |
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,3 @@ | ||
export interface IdAdapter { | ||
gen: () => 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,20 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { GenInput, GenOutput, TokenAdapter } from '../token'; | ||
import { sign } from 'jsonwebtoken'; | ||
|
||
@Injectable() | ||
export class JWTAdapter implements TokenAdapter { | ||
gen({ id }: GenInput): GenOutput { | ||
const accessToken = sign( | ||
{ | ||
sub: id, | ||
}, | ||
process.env['JWT_SECRET'], | ||
); | ||
|
||
return { | ||
accessToken, | ||
expiresAt: '', | ||
}; | ||
} | ||
} |
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,49 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { FileAdapter, GetInput, SaveInput } from '../file'; | ||
import { | ||
GetObjectCommand, | ||
PutObjectCommand, | ||
S3Client, | ||
} from '@aws-sdk/client-s3'; | ||
import { Readable } from 'stream'; | ||
|
||
@Injectable() | ||
export class S3Adapter implements FileAdapter { | ||
private client: S3Client; | ||
|
||
constructor() { | ||
this.client = new S3Client({ | ||
endpoint: process.env['AWS_ENDPOINT'], | ||
region: process.env['AWS_DEFAULT_REGION'], | ||
credentials: { | ||
secretAccessKey: process.env['AWS_SECRET_ACCESS_KEY'], | ||
accessKeyId: process.env['AWS_ACCESS_KEY_ID'], | ||
}, | ||
forcePathStyle: process.env['NODE_ENV'] !== 'production', | ||
}); | ||
} | ||
|
||
async save({ folder, filePath, file, metadata }: SaveInput) { | ||
await this.client.send( | ||
new PutObjectCommand({ | ||
Bucket: folder, | ||
Key: filePath.replace(/^\//, ''), | ||
Body: file, | ||
Metadata: metadata, | ||
}), | ||
); | ||
|
||
return filePath; | ||
} | ||
|
||
async getReadStream({ folder, filePath }: GetInput): Promise<Readable> { | ||
const file = await this.client.send( | ||
new GetObjectCommand({ | ||
Bucket: folder, | ||
Key: filePath.replace(/^\//, ''), | ||
}), | ||
); | ||
|
||
return file.Body! as Readable; | ||
} | ||
} |
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 { Injectable } from '@nestjs/common'; | ||
import { IsLatestInput, LatestInput, VersionAdapter } from '../version'; | ||
|
||
import { gt, rsort } from 'semver'; | ||
|
||
@Injectable() | ||
export class SemVerAdapter implements VersionAdapter { | ||
latest({ versions }: LatestInput): string { | ||
const [latestSemVer] = rsort(versions); | ||
|
||
return latestSemVer; | ||
} | ||
|
||
isGt({ toValidate, compareWith }: IsLatestInput): boolean { | ||
if (!compareWith) return true; | ||
if (!toValidate) return false; | ||
|
||
return gt(toValidate, compareWith); | ||
} | ||
} |
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,35 @@ | ||
import { SESClient, SendEmailCommand } from '@aws-sdk/client-ses'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { EmailAdapter, SendInput } from '../email'; | ||
|
||
@Injectable() | ||
export class SESAdapter implements EmailAdapter { | ||
private client: SESClient; | ||
|
||
constructor() { | ||
this.client = new SESClient(); | ||
} | ||
|
||
async send({ from, to, title, body }: SendInput) { | ||
await this.client.send( | ||
new SendEmailCommand({ | ||
Source: from, | ||
Destination: { | ||
ToAddresses: [to], | ||
}, | ||
Message: { | ||
Subject: { | ||
Data: title, | ||
Charset: 'UTF-8', | ||
}, | ||
Body: { | ||
Html: { | ||
Data: body, | ||
Charset: 'UTF-8', | ||
}, | ||
}, | ||
}, | ||
}), | ||
); | ||
} | ||
} |
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 @@ | ||
import { uid } from 'uid/single'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { SecretAdapter } from '../secret'; | ||
|
||
@Injectable() | ||
export class UIDSecretAdapter implements SecretAdapter { | ||
gen(): string { | ||
return uid(32); | ||
} | ||
} |
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 @@ | ||
import { uid } from 'uid/secure'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { IdAdapter } from '../id'; | ||
|
||
@Injectable() | ||
export class UIDAdapter implements IdAdapter { | ||
gen(): string { | ||
return uid(16); | ||
} | ||
} |
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,49 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { | ||
PaginationInput, | ||
PaginationOutput, | ||
UtilsAdapter as UtilsAdapterType, | ||
} from '../utils'; | ||
|
||
@Injectable() | ||
export class UtilsAdapter implements UtilsAdapterType { | ||
pagination({ | ||
page: originalPage, | ||
limit: originalLimit, | ||
}: PaginationInput): PaginationOutput { | ||
const page = originalPage || 1; | ||
const limit = originalLimit || 15; | ||
|
||
return { | ||
paging: { | ||
curPage: page, | ||
nextPage: page + 1, | ||
prevPage: page === 1 ? undefined : page - 1, | ||
limit, | ||
}, | ||
limit, | ||
offset: (page - 1) * limit, | ||
}; | ||
} | ||
|
||
formatMoney(valueNumber: number): string { | ||
const value = valueNumber.toString(); | ||
|
||
const decimalsStart = value.length - 2; | ||
|
||
const formatter = new Intl.NumberFormat('pt-BR', { | ||
style: 'currency', | ||
currency: 'BRL', | ||
}); | ||
|
||
return formatter.format( | ||
parseFloat( | ||
[ | ||
value.substring(0, decimalsStart), | ||
'.', | ||
value.substring(decimalsStart), | ||
].join(''), | ||
), | ||
); | ||
} | ||
} |
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,3 @@ | ||
export interface SecretAdapter { | ||
gen: () => 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,20 @@ | ||
export interface TokenPayload { | ||
sub: string; // Account ID | ||
} | ||
|
||
export interface UserData { | ||
accountId: string; | ||
} | ||
|
||
export interface GenInput { | ||
id: string; | ||
} | ||
|
||
export interface GenOutput { | ||
accessToken: string; | ||
expiresAt: string; // ISO date | ||
} | ||
|
||
export interface TokenAdapter { | ||
gen: (i: GenInput) => GenOutput; | ||
} |
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 { PaginatedItems } from 'src/types/paginated-items'; | ||
|
||
export interface PaginationInput { | ||
page?: number; | ||
limit?: number; | ||
} | ||
|
||
export interface PaginationOutput { | ||
paging: PaginatedItems<any>['paging']; | ||
offset: number; | ||
limit: number; | ||
} | ||
|
||
export interface UtilsAdapter { | ||
pagination: (i: PaginationInput) => PaginationOutput; | ||
|
||
formatMoney: (i: number) => 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,14 @@ | ||
export interface LatestInput { | ||
versions: Array<string>; | ||
} | ||
|
||
export interface IsLatestInput { | ||
toValidate: string | undefined; | ||
compareWith: string | undefined; | ||
} | ||
|
||
export interface VersionAdapter { | ||
latest: (i: LatestInput) => string; | ||
|
||
isGt: (i: IsLatestInput) => boolean; | ||
} |
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,27 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AccountModule } from './usecases/account/account.module'; | ||
import { TermsModule } from './usecases/terms/terms.module'; | ||
import { DocumentModule } from './usecases/document/document.module'; | ||
import { NotificationModule } from './usecases/notification/notification.module'; | ||
import { StoreModule } from './usecases/store/store.module'; | ||
import { ProductModule } from './usecases/product/product.module'; | ||
import { ContentModule } from './usecases/content/content.module'; | ||
import { SaleModule } from './usecases/sale/sale.module'; | ||
import { TransactionModule } from './usecases/transaction/transaction.module'; | ||
import { PostgresModule } from './repositories/postgres'; | ||
|
||
@Module({ | ||
imports: [ | ||
PostgresModule, | ||
AccountModule, | ||
TermsModule, | ||
DocumentModule, | ||
NotificationModule, | ||
StoreModule, | ||
ProductModule, | ||
ContentModule, | ||
SaleModule, | ||
TransactionModule, | ||
], | ||
}) | ||
export class AppModule {} |
Oops, something went wrong.