Skip to content

Commit

Permalink
feat(orders): add service to list all orders and end point to list all
Browse files Browse the repository at this point in the history
  • Loading branch information
demmorou committed Mar 22, 2021
1 parent cbfc589 commit e8d0745
Show file tree
Hide file tree
Showing 29 changed files with 530 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
NODE_ENV=development
PORT=3333

# MONGO
MONGODB_URL=

# BLING
BLING_API_KEY=
BLING_API_URL=https://bling.com.br/Api/v2
Expand Down
6 changes: 3 additions & 3 deletions babel.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
"module-resolver",
{
"alias": {
"@config": "./src/config",
"@modules": "./src/modules",
"@shared": "./src/shared"
"~config": "./src/config",
"~modules": "./src/modules",
"~shared": "./src/shared"
}
}
]
Expand Down
13 changes: 13 additions & 0 deletions ormconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const rootDir = process.env.NODE_ENV === 'production' ? './dist' : './src';

module.exports = [
{
name: 'default',
type: 'mongodb',
url: process.env.MONGODB_URL,
useNewUrlParser: true,
logging: !!(process.env.NODE_ENV !== 'production'),
useUnifiedTopology: true,
entities: [`${rootDir}/modules/**/infra/typeorm/schemas/*.{js,ts}`]
}
]
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build": "babel src --extensions \".js,.ts\" --out-dir dist --copy-files --no-copy-ignored",
"dev": "tsnd --poll -r tsconfig-paths/register --transpile-only --ignore-watch node_modules --no-notify src/shared/infra/http/server.ts",
"start": "node dist/shared/infra/http/server.js",
"typeorm": "./node_modules/typeorm/cli.js",
"test": "jest"
},
"config": {
Expand All @@ -16,13 +17,16 @@
},
"dependencies": {
"axios": "^0.21.1",
"date-fns": "^2.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"form-data": "^4.0.0",
"json2xml": "^0.1.3",
"mongodb": "^3.6.5",
"node-cron": "^3.0.0",
"reflect-metadata": "^0.1.13",
"tsyringe": "^4.4.0"
"tsyringe": "^4.4.0",
"typeorm": "^0.2.31"
},
"devDependencies": {
"@babel/cli": "^7.13.10",
Expand All @@ -37,6 +41,7 @@
"@types/express": "^4.17.11",
"@types/form-data": "^2.5.0",
"@types/jest": "^26.0.15",
"@types/mongodb": "^3.6.10",
"@types/node-cron": "^1.2.0",
"@typescript-eslint/eslint-plugin": "^4.6.0",
"@typescript-eslint/parser": "^4.6.0",
Expand Down
6 changes: 6 additions & 0 deletions src/modules/bling/dtos/ICreateOrderDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface ICreateOrderDTO {
date: string;
value: number;
}

export { ICreateOrderDTO };
16 changes: 16 additions & 0 deletions src/modules/bling/infra/http/controllers/OrdersController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Response, Request } from 'express';
import { container } from 'tsyringe';

import { ListOrdersService } from '~modules/bling/services/ListOrdersService';

class OrdersController {
public async index(request: Request, response: Response): Promise<Response> {
const listOrders = container.resolve(ListOrdersService);

const orders = await listOrders.execute();

return response.json(orders);
}
}

export { OrdersController };
11 changes: 11 additions & 0 deletions src/modules/bling/infra/http/routes/orders.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Router } from 'express';

import { OrdersController } from '../controllers/OrdersController';

const orderRoutes = Router();

const ordersController = new OrdersController();

orderRoutes.get('/', ordersController.index);

export { orderRoutes };
32 changes: 32 additions & 0 deletions src/modules/bling/infra/typeorm/repositories/OrdersRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getMongoRepository, MongoRepository, ObjectLiteral } from 'typeorm';

import { ICreateOrderDTO } from '~modules/bling/dtos/ICreateOrderDTO';
import { IOrdersRepository } from '~modules/bling/repositories/IOrdersRepository';

import { Order } from '../schemas/Order';

class OrdersRepository implements IOrdersRepository {
private ormRepository: MongoRepository<Order>;

constructor() {
this.ormRepository = getMongoRepository(Order, 'default');
}

public async create({ date, value }: ICreateOrderDTO): Promise<void> {
const update: ObjectLiteral = {
$set: { value: { $sum: ['$value', value] } },
};

await this.ormRepository.updateOne({ date }, [update], {
upsert: true,
});
}

public async find(): Promise<Order[]> {
const orders = await this.ormRepository.find();

return orders;
}
}

export { OrdersRepository };
14 changes: 14 additions & 0 deletions src/modules/bling/infra/typeorm/schemas/Order.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Entity, Column } from 'typeorm';

import { BaseSchema } from '~shared/infra/typeorm/schemas/BaseSchema';

@Entity('orders')
class Order extends BaseSchema {
@Column()
date: string;

@Column()
value: number;
}

export { Order };
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ import { ScheduledTask, schedule } from 'node-cron';
import { container } from 'tsyringe';

import { CreateOrderService } from '~modules/bling/services/CreateOrderService';

import { SearchDealsService } from '../services/SearchDealsService';
import { SearchDealsService } from '~modules/pipedrive/services/SearchDealsService';

class GetDealsAndCreateOrders {
public async run(): Promise<void> {
await this.job();
}

private async job(): Promise<ScheduledTask> {
return schedule('50 12 * * *', async () => {
return schedule('40 06 23 * * *', async () => {
console.log('stage 1');

const getDeals = container.resolve(SearchDealsService);
const deals = await getDeals.execute({ start: 0, status: 'won' });
console.log('stage 2');

const createOrder = container.resolve(CreateOrderService);
await createOrder.execute(deals.data);
console.log('stage 3');
});
}
}
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions src/modules/bling/providers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { container } from 'tsyringe';

import { OrdersRepository } from '../infra/typeorm/repositories/OrdersRepository';
import { IOrdersRepository } from '../repositories/IOrdersRepository';

container.registerSingleton<IOrdersRepository>(
'OrdersRepository',
OrdersRepository,
);
9 changes: 9 additions & 0 deletions src/modules/bling/repositories/IOrdersRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ICreateOrderDTO } from '../dtos/ICreateOrderDTO';
import { Order } from '../infra/typeorm/schemas/Order';

interface IOrdersRepository {
create(data: ICreateOrderDTO): Promise<void>;
find(): Promise<Order[]>;
}

export { IOrdersRepository };
21 changes: 16 additions & 5 deletions src/modules/bling/services/CreateOrderService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { AxiosResponse } from 'axios';
import { format, parseISO } from 'date-fns';
import FormData from 'form-data';
import json2xml from 'json2xml';
import { inject, injectable } from 'tsyringe';
Expand All @@ -9,11 +9,15 @@ import { IHttpProvider } from '~shared/container/providers/HttpProvider/models/I
import { blingConfig } from '~config/bling';
import { IDealData } from '~modules/pipedrive/dtos/IGetDealsResponseDTO';

import { IOrdersRepository } from '../repositories/IOrdersRepository';

@injectable()
export class CreateOrderService {
class CreateOrderService {
constructor(
@inject('HttpProvider')
private readonly httpProvider: IHttpProvider,
@inject('OrdersRepository')
private ordersRepository: IOrdersRepository,
) {
httpProvider.setup({
baseURL: blingConfig.baseURL,
Expand All @@ -28,8 +32,10 @@ export class CreateOrderService {

deals.forEach(async deal => {
const formData = new FormData();
const date = format(parseISO(deal.won_time), 'dd-MM-yyyy');

const pedido = {
data: date,
cliente: {
nome: deal.person_id.name,
},
Expand All @@ -47,15 +53,20 @@ export class CreateOrderService {

formData.append('xml', xml);

await this.ordersRepository.create({
date,
value: deal.value,
});

promises.push(
this.httpProvider.post('/pedido/json/', formData, {
headers: formData.getHeaders(),
}),
);
});

Promise.all(promises).then(response => {
console.log(response);
});
await Promise.all(promises);
}
}

export { CreateOrderService };
20 changes: 20 additions & 0 deletions src/modules/bling/services/ListOrdersService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { inject, injectable } from 'tsyringe';

import { Order } from '../infra/typeorm/schemas/Order';
import { IOrdersRepository } from '../repositories/IOrdersRepository';

@injectable()
class ListOrdersService {
constructor(
@inject('OrdersRepository')
private readonly ordersRepository: IOrdersRepository,
) {}

public async execute(): Promise<Order[]> {
const orders = await this.ordersRepository.find();

return orders;
}
}

export { ListOrdersService };
7 changes: 5 additions & 2 deletions src/modules/pipedrive/dtos/IGetDealsResponseDTO.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
export interface IDealData {
interface IDealData {
id: string;
won_time: string;
title: string;
value: number;
person_id: {
name: string;
};
}

export interface IGetDealsResponseDTO {
interface IGetDealsResponseDTO {
success: boolean;
data: IDealData[];
}

export { IDealData, IGetDealsResponseDTO };
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface ISearchQuery {
start: number;
}

export class DealsController {
class DealsController {
public async index(request: Request, response: Response): Promise<Response> {
const { status, start } = (request.query as unknown) as ISearchQuery;

Expand All @@ -19,3 +19,5 @@ export class DealsController {
return response.json(deals);
}
}

export { DealsController };
4 changes: 3 additions & 1 deletion src/modules/pipedrive/services/SearchDealsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface IRequest {
}

@injectable()
export class SearchDealsService {
class SearchDealsService {
constructor(
@inject('HttpProvider')
private readonly httpProvider: IHttpProvider,
Expand Down Expand Up @@ -42,3 +42,5 @@ export class SearchDealsService {
return response.data;
}
}

export { SearchDealsService };
2 changes: 1 addition & 1 deletion src/shared/container/jobs/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import '~modules/pipedrive/jobs';
import '~modules/bling/jobs';
4 changes: 3 additions & 1 deletion src/shared/container/providers/HttpProvider/dtos/IHeaders.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export interface IHeaders {
interface IHeaders {
headers?: Record<string, unknown>;
}

export { IHeaders };
4 changes: 3 additions & 1 deletion src/shared/container/providers/HttpProvider/dtos/ISetup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { IHeaders } from './IHeaders';

export interface IHttpSetup extends IHeaders {
interface IHttpSetup extends IHeaders {
baseURL: string;
params?: any;
}

export { IHttpSetup };
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { IHttpSetup } from '../dtos/ISetup';
import { IHttpProvider } from '../models/IHttpProvider';

export class AxiosProvider implements IHttpProvider {
class AxiosProvider implements IHttpProvider {
private axios: AxiosInstance;

public setup({ baseURL, headers, params }: IHttpSetup): void {
Expand Down Expand Up @@ -54,3 +54,5 @@ export class AxiosProvider implements IHttpProvider {
return this.axios.patch(path, data, config);
}
}

export { AxiosProvider };
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AxiosRequestConfig, AxiosResponse } from 'axios';

import { IHttpSetup } from '../dtos/ISetup';

export interface IHttpProvider {
interface IHttpProvider {
setup(data: IHttpSetup): void;
get<T = any>(
path: string,
Expand All @@ -29,3 +29,5 @@ export interface IHttpProvider {
config?: AxiosRequestConfig,
): Promise<AxiosResponse<T>>;
}

export { IHttpProvider };
2 changes: 2 additions & 0 deletions src/shared/container/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import './HttpProvider';

import '~modules/bling/providers';
2 changes: 2 additions & 0 deletions src/shared/infra/http/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Router } from 'express';

import { orderRoutes } from '~modules/bling/infra/http/routes/orders.routes';
import { dealsRouter } from '~modules/pipedrive/infra/http/routes/deals.routes';

const router = Router();

router.use('/deals', dealsRouter);
router.use('/orders', orderRoutes);

export { router };
Loading

0 comments on commit e8d0745

Please sign in to comment.