Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/app/operations/product/createProductOperation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
module.exports = ({ createProductUsecase }) => ({
module.exports = ({ createProductUsecase, createWarehouseUsecase, bindingProductWarehouse }) => ({
execute: async (data) => {
const response = await createProductUsecase.execute(data);
return response;
const { warehouse } = data;
const product = await createProductUsecase.execute(data);
if (!product) {
return;
}
if (warehouse) {
const warehouseCretead = await Promise.all(warehouse.map((element) => createWarehouseUsecase.execute(element)));
// TODO: validate warehouseCreated
await bindingProductWarehouse.execute(product.id, warehouseCretead);
}
return product;
}
});
15 changes: 15 additions & 0 deletions src/app/usecases/bindingProductWarehouse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = ({ createProductWarehouseUsecase }) => ({
execute: async (productId, warehouseIds) => {
const bind = [];
if (Array.isArray(warehouseIds) === false) {
const created = await createProductWarehouseUsecase.execute({ productId, warehouseIds });
bind.push(created);
return bind;
}
for (const { id } of warehouseIds) {
const created = await createProductWarehouseUsecase.execute({ productId, warehouseId: id });
bind.push(created);
}
return bind;
}
});
6 changes: 6 additions & 0 deletions src/app/usecases/createProductWarehouseUsecase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = ({ productWarehouseRepository }) => ({
execute: async (data) => {
const response = await productWarehouseRepository.create(data);
return response;
}
});
6 changes: 6 additions & 0 deletions src/app/usecases/createWarehouseUsecase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = ({ warehouseRepository }) => ({
execute: async (data) => {
const response = await warehouseRepository.create(data);
return response;
}
});
9 changes: 8 additions & 1 deletion src/domain/entities/Warehouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ const Warehouse = attributes({
type: { ...types.required_number }
})(class Warehouse {});

module.exports = Warehouse;
const WarehouseOutput = attributes({
id: { ...types.required_number },
locality: { ...types.required_string },
quantity: Number,
type: { ...types.required_number }
})(class Warehouse {});

module.exports = { Warehouse, WarehouseOutput };
1 change: 0 additions & 1 deletion src/infra/repositories/Repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class Repository {
async create(domainEntity) {
const model = await new this.ResourceModel(this.ResourceMapper.toInputDatabase(domainEntity));
const dbCreatedResource = await model.save();
console.log('dbCreatedResource', dbCreatedResource);
return this.ResourceMapper.toOutputDabase(dbCreatedResource);
}

Expand Down
13 changes: 13 additions & 0 deletions src/infra/repositories/productWarehouse/ProductWarehouseMapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// const { Warehouse, WarehouseOutput } = require('../../../domain/entities/Warehouse');

const ProductWarehouseMapper = {
toInputDatabase(entry) {
console.log(entry);
return entry;
},
toOutputDabase({ dataValues }) {
return dataValues;
}
};

module.exports = () => ProductWarehouseMapper;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const Repository = require('../Repository');

class WarehouseRepository extends Repository {
constructor({ sequelize, productWarehouseMapper, exceptions }) {
const { ProductWarehouse } = sequelize.models;
super({
ResourceModel: ProductWarehouse,
ResourceMapper: productWarehouseMapper,
Exceptions: exceptions
});
}
}

module.exports = WarehouseRepository;
12 changes: 12 additions & 0 deletions src/infra/repositories/warehouse/WarehouseMapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { Warehouse, WarehouseOutput } = require('../../../domain/entities/Warehouse');

const WarehouseMapper = {
toInputDatabase(entry) {
return new Warehouse({ ...entry }).toJSON();
},
toOutputDabase({ dataValues }) {
return new WarehouseOutput({ ...dataValues }).toJSON();
}
};

module.exports = () => WarehouseMapper;
14 changes: 14 additions & 0 deletions src/infra/repositories/warehouse/WarehouseRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const Repository = require('../Repository');

class WarehouseRepository extends Repository {
constructor({ sequelize, warehouseMapper, exceptions }) {
const { Warehouse } = sequelize.models;
super({
ResourceModel: Warehouse,
ResourceMapper: warehouseMapper,
Exceptions: exceptions
});
}
}

module.exports = WarehouseRepository;