Füge neue Modelle für Produktion, Inventar und kaufbare Bestände hinzu; aktualisiere bestehende Modelle und Routen
This commit is contained in:
@@ -7,11 +7,13 @@ class FalukantController {
|
||||
this.randomFirstName = this.randomFirstName.bind(this);
|
||||
this.randomLastName = this.randomLastName.bind(this);
|
||||
this.getInfo = this.getInfo.bind(this);
|
||||
this.getInventory = this.getInventory.bind(this);
|
||||
this.sellProduct = this.sellProduct.bind(this);
|
||||
}
|
||||
|
||||
async getUser(req, res) {
|
||||
try {
|
||||
const { userid: hashedUserId } = req.headers;
|
||||
const { userid: hashedUserId } = req.headers;
|
||||
const result = await FalukantService.getUser(hashedUserId);
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
@@ -69,6 +71,94 @@ class FalukantController {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async getBranch(req, res) {
|
||||
try {
|
||||
const { userid: hashedUserId } = req.headers;
|
||||
const { branch: branchId } = req.params;
|
||||
console.log(branchId, req.params);
|
||||
const result = await FalukantService.getBranch(hashedUserId, branchId);
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async createProduction(req, res) {
|
||||
try {
|
||||
const { userid: hashedUserId } = req.headers;
|
||||
const { branchId, productId, quantity } = req.body;
|
||||
const result = await FalukantService.createProduction(hashedUserId, branchId, productId, quantity);
|
||||
res.status(201).json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async getProduction(req, res) {
|
||||
try {
|
||||
const { userid: hashedUserId } = req.headers;
|
||||
const { branchId } = req.params;
|
||||
const result = await FalukantService.getProduction(hashedUserId, branchId);
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async getStock(req, res) {
|
||||
try {
|
||||
const { userid: hashedUserId } = req.headers;
|
||||
const { branchId } = req.params || null;
|
||||
const result = await FalukantService.getStock(hashedUserId, branchId);
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async createStock(req, res) {
|
||||
try {
|
||||
const { userid: hashedUserId } = req.headers;
|
||||
const { branchId, stockTypeId, stockSize } = req.body;
|
||||
const result = await FalukantService.createStock(hashedUserId, branchId, stockTypeId, stockSize);
|
||||
res.status(201).json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async getProducts(req, res) {
|
||||
try {
|
||||
const { userid: hashedUserId } = req.headers;
|
||||
const result = await FalukantService.getProducts(hashedUserId);
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async getInventory(req, res) {
|
||||
try {
|
||||
const { userid: hashedUserId } = req.headers;
|
||||
const { branchId } = req.params;
|
||||
const result = await FalukantService.getInventory(hashedUserId, branchId);
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
|
||||
async sellProduct(req, res) {
|
||||
try {
|
||||
const { userid: hashedUserId } = req.headers;
|
||||
const { branchId, productId, quality, quantity } = req.body;
|
||||
const result = await FalukantService.sellProduct(hashedUserId, branchId, productId, quality, quantity);
|
||||
res.status(201).json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default FalukantController;
|
||||
|
||||
@@ -42,6 +42,9 @@ import TitleOfNobility from './falukant/type/title_of_nobility.js';
|
||||
import TitleRequirement from './falukant/type/title_requirement.js';
|
||||
import Branch from './falukant/data/branch.js';
|
||||
import BranchType from './falukant/type/branch.js';
|
||||
import Production from './falukant/data/production.js';
|
||||
import Inventory from './falukant/data/inventory.js';
|
||||
import BuyableStock from './falukant/data/buayble_stock.js';
|
||||
|
||||
|
||||
export default function setupAssociations() {
|
||||
@@ -205,21 +208,15 @@ export default function setupAssociations() {
|
||||
|
||||
FalukantCharacter.belongsTo(TitleOfNobility, { foreignKey: 'titleOfNobility', as: 'nobleTitle' });
|
||||
TitleOfNobility.hasMany(FalukantCharacter, { foreignKey: 'titleOfNobility', as: 'charactersWithNobleTitle' });
|
||||
|
||||
|
||||
FalukantCharacter.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
|
||||
RegionData.hasMany(FalukantCharacter, { foreignKey: 'regionId', as: 'charactersInRegion' });
|
||||
|
||||
FalukantStock.belongsTo(FalukantStockType, { foreignKey: 'stockTypeId', as: 'stockType' });
|
||||
FalukantStockType.hasMany(FalukantStock, { foreignKey: 'stockTypeId', as: 'stocks' });
|
||||
|
||||
FalukantStock.belongsTo(FalukantUser, { foreignKey: 'userId', as: 'user' });
|
||||
FalukantUser.hasMany(FalukantStock, { foreignKey: 'userId', as: 'stocks' });
|
||||
|
||||
FalukantStock.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
|
||||
RegionData.hasMany(FalukantStock, { foreignKey: 'regionId', as: 'stocksInRegion' });
|
||||
|
||||
Knowledge.belongsTo(ProductType, { foreignKey: 'productTypeId', as: 'productType' });
|
||||
ProductType.hasMany(Knowledge, { foreignKey: 'productTypeId', as: 'knowledges' });
|
||||
Knowledge.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
|
||||
ProductType.hasMany(Knowledge, { foreignKey: 'productId', as: 'knowledges' });
|
||||
|
||||
Knowledge.belongsTo(FalukantCharacter, { foreignKey: 'characterId', as: 'character' });
|
||||
FalukantCharacter.hasMany(Knowledge, { foreignKey: 'characterId', as: 'knowledges' });
|
||||
@@ -235,4 +232,23 @@ export default function setupAssociations() {
|
||||
|
||||
Branch.belongsTo(BranchType, { foreignKey: 'branchTypeId', as: 'branchType' });
|
||||
BranchType.hasMany(Branch, { foreignKey: 'branchTypeId', as: 'branches' });
|
||||
|
||||
Production.belongsTo(Branch, { foreignKey: 'branchId', as: 'branch' });
|
||||
Branch.hasMany(Production, { foreignKey: 'branchId', as: 'productions' });
|
||||
|
||||
Production.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
|
||||
ProductType.hasMany(Production, { foreignKey: 'productId', as: 'productions' });
|
||||
|
||||
Inventory.belongsTo(FalukantStock, { foreignKey: 'stockId', as: 'stock' });
|
||||
FalukantStock.hasMany(Inventory, { foreignKey: 'stockId', as: 'inventories' });
|
||||
|
||||
Inventory.belongsTo(ProductType, { foreignKey: 'productId', as: 'productType' });
|
||||
ProductType.hasMany(Inventory, { foreignKey: 'productId', as: 'inventories' });
|
||||
|
||||
BuyableStock.belongsTo(RegionData, { foreignKey: 'regionId', as: 'region' });
|
||||
RegionData.hasMany(BuyableStock, { foreignKey: 'regionId', as: 'buyableStocks' });
|
||||
|
||||
Branch.hasMany(FalukantStock, { foreignKey: 'branchId', as: 'stocks' });
|
||||
FalukantStock.belongsTo(Branch, { foreignKey: 'branchId', as: 'branch' });
|
||||
|
||||
}
|
||||
|
||||
28
backend/models/falukant/data/buayble_stock.js
Normal file
28
backend/models/falukant/data/buayble_stock.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { Model, DataTypes } from 'sequelize';
|
||||
import { sequelize } from '../../../utils/sequelize.js';
|
||||
|
||||
class BuyableStock extends Model { }
|
||||
|
||||
BuyableStock.init({
|
||||
regionId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
stockTypeId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
quantity: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: 'BuyableStock',
|
||||
tableName: 'buyable_stock',
|
||||
schema: 'falukant_data',
|
||||
timestamps: false,
|
||||
underscored: true,
|
||||
});
|
||||
|
||||
export default BuyableStock;
|
||||
37
backend/models/falukant/data/inventory.js
Normal file
37
backend/models/falukant/data/inventory.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import { Model, DataTypes } from 'sequelize';
|
||||
import { sequelize } from '../../../utils/sequelize.js';
|
||||
|
||||
class Inventory extends Model { }
|
||||
|
||||
Inventory.init({
|
||||
stockId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
productId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
quantity: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
quality: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
producedAt: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: false,
|
||||
defaultValue: DataTypes.NOW,
|
||||
}
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: 'Inventory',
|
||||
tableName: 'inventory',
|
||||
schema: 'falukant_data',
|
||||
timestamps: false,
|
||||
underscored: true,
|
||||
});
|
||||
|
||||
export default Inventory;
|
||||
33
backend/models/falukant/data/production.js
Normal file
33
backend/models/falukant/data/production.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Model, DataTypes } from 'sequelize';
|
||||
import { sequelize } from '../../../utils/sequelize.js';
|
||||
|
||||
class Production extends Model { }
|
||||
|
||||
Production.init({
|
||||
branchId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
productId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
quantity: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
startTimestamp: {
|
||||
type: DataTypes.DATE,
|
||||
allowNull: false,
|
||||
defaultValue: DataTypes.NOW,
|
||||
}
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: 'Production',
|
||||
tableName: 'production',
|
||||
schema: 'falukant_data',
|
||||
timestamps: false,
|
||||
underscored: true,
|
||||
});
|
||||
|
||||
export default Production;
|
||||
@@ -4,13 +4,10 @@ import { sequelize } from '../../../utils/sequelize.js';
|
||||
class FalukantStock extends Model { }
|
||||
|
||||
FalukantStock.init({
|
||||
userId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
},
|
||||
regionId: {
|
||||
branchId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
defaultValue: 0
|
||||
},
|
||||
stockTypeId: {
|
||||
type: DataTypes.INTEGER,
|
||||
|
||||
@@ -37,6 +37,11 @@ FalukantUser.init({
|
||||
allowNull: false,
|
||||
defaultValue: 0.00,
|
||||
},
|
||||
certificate: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
defaultValue: 1,
|
||||
},
|
||||
mainBranchRegionId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: true,
|
||||
|
||||
@@ -46,6 +46,9 @@ import TitleRequirement from './falukant/type/title_requirement.js';
|
||||
import TitleOfNobility from './falukant/type/title_of_nobility.js';
|
||||
import BranchType from './falukant/type/branch.js';
|
||||
import Branch from './falukant/data/branch.js';
|
||||
import Production from './falukant/data/production.js';
|
||||
import Inventory from './falukant/data/inventory.js';
|
||||
import BuyableStock from './falukant/data/buayble_stock.js';
|
||||
|
||||
const models = {
|
||||
SettingsType,
|
||||
@@ -54,10 +57,10 @@ const models = {
|
||||
UserRightType,
|
||||
User,
|
||||
UserParam,
|
||||
Login,
|
||||
Login,
|
||||
UserRight,
|
||||
InterestType,
|
||||
InterestTranslationType,
|
||||
InterestTranslationType,
|
||||
Interest,
|
||||
ContactMessage,
|
||||
UserParamVisibilityType,
|
||||
@@ -96,6 +99,9 @@ const models = {
|
||||
TitleRequirement,
|
||||
BranchType,
|
||||
Branch,
|
||||
Production,
|
||||
Inventory,
|
||||
BuyableStock,
|
||||
};
|
||||
|
||||
export default models;
|
||||
|
||||
@@ -9,5 +9,13 @@ router.post('/user', falukantController.createUser);
|
||||
router.get('/name/randomfirstname/:gender', falukantController.randomFirstName);
|
||||
router.get('/name/randomlastname', falukantController.randomLastName);
|
||||
router.get('/info', falukantController.getInfo);
|
||||
router.get('/branches/:branch', falukantController.getBranch);
|
||||
router.get('/branches', falukantController.getBranches);
|
||||
router.post('/production', falukantController.createProduction);
|
||||
router.get('/production/:branchId', falukantController.getProduction);
|
||||
router.get('/stock/?:branchId', falukantController.getStock);
|
||||
router.post('/stock', falukantController.createStock);
|
||||
router.get('/products', falukantController.getProducts);
|
||||
router.get('/inventory/?:branchId', falukantController.getInventory);
|
||||
router.post('/sell', falukantController.sellProduct);
|
||||
export default router;
|
||||
|
||||
@@ -14,6 +14,12 @@ import { differenceInDays } from 'date-fns';
|
||||
import TitleOfNobility from '../models/falukant/type/title_of_nobility.js';
|
||||
import Branch from '../models/falukant/data/branch.js';
|
||||
import BranchType from '../models/falukant/type/branch.js';
|
||||
import Production from '../models/falukant/data/production.js';
|
||||
import ProductType from '../models/falukant/type/product.js';
|
||||
import { Op, fn, col } from 'sequelize';
|
||||
import Knowledge from '../models/falukant/data/product_knowledge.js';
|
||||
import Inventory from '../models/falukant/data/inventory.js';
|
||||
import Stock from '../models/falukant/data/stock.js';
|
||||
|
||||
class FalukantService extends BaseService {
|
||||
async getFalukantUserByHashedId(hashedId) {
|
||||
@@ -26,7 +32,7 @@ class FalukantService extends BaseService {
|
||||
hashedId: hashedId
|
||||
},
|
||||
}
|
||||
],
|
||||
],
|
||||
});
|
||||
return falukantUser;
|
||||
}
|
||||
@@ -257,19 +263,19 @@ class FalukantService extends BaseService {
|
||||
{
|
||||
model: BranchType,
|
||||
as: 'branchType',
|
||||
attributes: ['labelTr'],
|
||||
attributes: ['labelTr'],
|
||||
},
|
||||
{
|
||||
model: RegionData,
|
||||
as: 'region',
|
||||
attributes: ['name'],
|
||||
attributes: ['name'],
|
||||
}
|
||||
],
|
||||
attributes: ['id', 'regionId'],
|
||||
order: [['branchTypeId', 'ASC']],
|
||||
attributes: ['id', 'regionId'],
|
||||
order: [['branchTypeId', 'ASC']],
|
||||
});
|
||||
const enrichedBranches = branches.map(branch => ({
|
||||
...branch.toJSON(),
|
||||
...branch.toJSON(),
|
||||
isMainBranch: falukantUser.mainBranchRegionId === branch.regionId,
|
||||
}));
|
||||
return enrichedBranches;
|
||||
@@ -279,6 +285,292 @@ class FalukantService extends BaseService {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
async getBranch(hashedUserId, branchId) {
|
||||
try {
|
||||
const falukantUser = await this.getFalukantUserByHashedId(hashedUserId);
|
||||
if (!falukantUser) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
const branch = await Branch.findOne({
|
||||
where: { id: branchId, falukantUserId: falukantUser.id },
|
||||
include: [
|
||||
{
|
||||
model: BranchType,
|
||||
as: 'branchType',
|
||||
attributes: ['labelTr'],
|
||||
},
|
||||
{
|
||||
model: RegionData,
|
||||
as: 'region',
|
||||
attributes: ['name'],
|
||||
},
|
||||
{
|
||||
model: Production,
|
||||
as: 'productions',
|
||||
attributes: ['quantity', 'startTimestamp'],
|
||||
include: [
|
||||
{
|
||||
model: ProductType,
|
||||
as: 'productType',
|
||||
attributes: ['id', 'category', 'labelTr', 'sellCost', 'productionTime'],
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
attributes: ['id', 'regionId'],
|
||||
});
|
||||
if (!branch) {
|
||||
throw new Error('Branch not found');
|
||||
}
|
||||
return branch;
|
||||
} catch (error) {
|
||||
console.error('Error in getBranch:', error);
|
||||
throw new Error('Failed to retrieve branch');
|
||||
}
|
||||
}
|
||||
|
||||
async getStock(hashedUserId, branchId) {
|
||||
try {
|
||||
const falukantUser = await this.getFalukantUserByHashedId(hashedUserId);
|
||||
if (!falukantUser) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
const branch = await Branch.findOne({ where: { id: branchId, falukantUserId: falukantUser.id } });
|
||||
if (!branch) {
|
||||
throw new Error('Branch not found');
|
||||
}
|
||||
const stock = await FalukantStock.findOne({ where: { regionId: branch.regionId, userId: falukantUser.id } });
|
||||
return stock;
|
||||
} catch (error) {
|
||||
console.error('Error in getStock:', error);
|
||||
throw new Error('Failed to retrieve stock');
|
||||
}
|
||||
}
|
||||
|
||||
async createStock(hashedUserId, branchId, stockData) {
|
||||
try {
|
||||
const falukantUser = await this.getFalukantUserByHashedId(hashedUserId);
|
||||
if (!falukantUser) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
const branch = await Branch.findOne({ where: { id: branchId, falukantUserId: falukantUser.id } });
|
||||
if (!branch) {
|
||||
throw new Error('Branch not found');
|
||||
}
|
||||
const stock = await FalukantStock.create({
|
||||
userId: falukantUser.id,
|
||||
regionId: branch.regionId,
|
||||
stockTypeId: stockData.stockTypeId,
|
||||
quantity: stockData.quantity,
|
||||
});
|
||||
return stock;
|
||||
} catch (error) {
|
||||
console.error('Error in createStock:', error);
|
||||
throw new Error('Failed to create stock');
|
||||
}
|
||||
}
|
||||
|
||||
async createProduction(hashedUserId, branchId, productId, quantity) {
|
||||
const falukantUser = await this.getFalukantUserByHashedId(hashedUserId);
|
||||
if (!falukantUser) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
|
||||
const branch = await Branch.findOne({ where: { id: branchId, falukantUserId: falukantUser.id } });
|
||||
if (!branch) {
|
||||
throw new Error('Branch not found');
|
||||
}
|
||||
const product = await ProductType.findOne({ where: { id: productId } });
|
||||
if (falukantUser.money < quantity * product.category * 7) {
|
||||
throw new Error('notenoughmoney');
|
||||
}
|
||||
const production = await Production.create({
|
||||
branchId: branch.id,
|
||||
productId: productId,
|
||||
quantity: quantity,
|
||||
});
|
||||
falukantUser.update({ money: falukantUser.money - quantity * product.category * 7 });
|
||||
notifyUser(falukantUser.user.hashedId, 'falukantUpdateStatus', {});
|
||||
notifyUser(falukantUser.user.hashedId, 'falukantBranchUpdate', { branchId: branch.id });
|
||||
return production;
|
||||
}
|
||||
|
||||
async getProduction(hashedUserId, branchId) {
|
||||
try {
|
||||
const falukantUser = await this.getFalukantUserByHashedId(hashedUserId);
|
||||
if (!falukantUser) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
const branch = await Branch.findOne({ where: { id: branchId, falukantUserId: falukantUser.id } });
|
||||
if (!branch) {
|
||||
throw new Error('Branch not found');
|
||||
}
|
||||
const production = await FalukantProduction.findOne({ where: { regionId: branch.regionId } });
|
||||
return production;
|
||||
} catch (error) {
|
||||
console.error('Error in getProduction:', error);
|
||||
throw new Error('Failed to retrieve production');
|
||||
}
|
||||
}
|
||||
|
||||
async getProducts(hashedUserId) {
|
||||
try {
|
||||
const falukantUser = await this.getFalukantUserByHashedId(hashedUserId);
|
||||
if (!falukantUser) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
const products = await ProductType.findAll({
|
||||
where: {
|
||||
category: {
|
||||
[Op.lte]: falukantUser.certificate
|
||||
}
|
||||
},
|
||||
include: [
|
||||
{
|
||||
model: Knowledge,
|
||||
as: 'knowledges',
|
||||
attributes: ['knowledge'],
|
||||
}
|
||||
],
|
||||
attributes: ['labelTr', 'id', 'sellCost', 'productionTime', 'category'],
|
||||
});
|
||||
return products;
|
||||
} catch (error) {
|
||||
console.error('Error in getProducts:', error);
|
||||
throw new Error('Failed to retrieve products');
|
||||
}
|
||||
}
|
||||
|
||||
async getInventory(hashedUserId, branchId) {
|
||||
try {
|
||||
const falukantUser = await this.getFalukantUserByHashedId(hashedUserId);
|
||||
if (!falukantUser) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
const branchFilter = branchId
|
||||
? { id: branchId, falukantUserId: falukantUser.id }
|
||||
: { falukantUserId: falukantUser.id };
|
||||
const branches = await Branch.findAll({
|
||||
where: branchFilter,
|
||||
include: [
|
||||
{
|
||||
model: FalukantStock,
|
||||
as: 'stocks',
|
||||
include: [
|
||||
{
|
||||
model: FalukantStockType,
|
||||
as: 'stockType',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
model: RegionData,
|
||||
as: 'region',
|
||||
include: [
|
||||
{
|
||||
model: RegionType,
|
||||
as: 'regionType',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
const stockIds = branches.flatMap(branch => branch.stocks.map(stock => stock.id));
|
||||
const inventoryItems = await Inventory.findAll({
|
||||
where: { stockId: stockIds },
|
||||
include: [
|
||||
{
|
||||
model: FalukantStock,
|
||||
as: 'stock',
|
||||
include: [
|
||||
{
|
||||
model: Branch,
|
||||
as: 'branch',
|
||||
include: [
|
||||
{
|
||||
model: RegionData,
|
||||
as: 'region',
|
||||
include: [
|
||||
{
|
||||
model: RegionType,
|
||||
as: 'regionType',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
model: FalukantStockType,
|
||||
as: 'stockType',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
model: ProductType,
|
||||
as: 'productType',
|
||||
},
|
||||
],
|
||||
});
|
||||
const groupedInventory = inventoryItems.reduce((acc, item) => {
|
||||
const region = item.stock.branch.region;
|
||||
const key = `${region.id}-${item.productType.id}-${item.quality}`;
|
||||
if (!acc[key]) {
|
||||
acc[key] = {
|
||||
region,
|
||||
product: item.productType,
|
||||
quality: item.quality,
|
||||
totalQuantity: 0,
|
||||
};
|
||||
}
|
||||
acc[key].totalQuantity += item.quantity;
|
||||
return acc;
|
||||
}, {});
|
||||
const sortedInventory = Object.values(groupedInventory).sort((a, b) => {
|
||||
if (a.region.id !== b.region.id) {
|
||||
return a.region.id - b.region.id;
|
||||
}
|
||||
if (a.product.id !== b.product.id) {
|
||||
return a.product.id - b.product.id;
|
||||
}
|
||||
return a.quality - b.quality;
|
||||
});
|
||||
|
||||
return sortedInventory;
|
||||
} catch (error) {
|
||||
console.error('Error in getInventory:', error);
|
||||
throw new Error('Failed to retrieve inventory');
|
||||
}
|
||||
}
|
||||
|
||||
async sellProduct(hashedUserId, branchId, productId, quality, quantity) {
|
||||
try {
|
||||
const falukantUser = await this.getFalukantUserByHashedId(hashedUserId);
|
||||
if (!falukantUser) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
const branch = await Branch.findOne({ where: { id: branchId, falukantUserId: falukantUser.id } });
|
||||
if (!branch) {
|
||||
throw new Error('Branch not found');
|
||||
}
|
||||
const product = await ProductType.findOne({ where: { id: productId } });
|
||||
if (!product) {
|
||||
throw new Error('Product not found');
|
||||
}
|
||||
const stock = await Stock.findOne({ where: { branchId: branch.id, } });
|
||||
if (!stock) {
|
||||
throw new Error('Stock not found');
|
||||
}
|
||||
if (stock.quantity < quantity) {
|
||||
throw new Error('Not enough stock');
|
||||
}
|
||||
await FalukantStock.decrement('quantity', { by: quantity, where: { regionId: branch.regionId, stockTypeId: product.id } });
|
||||
const inventory = await Inventory.create({ stockId: stock.id, quality, quantity });
|
||||
return inventory;
|
||||
} catch (error) {
|
||||
console.error('Error in sellProduct:', error);
|
||||
throw new Error('Failed to sell product');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new FalukantService();
|
||||
|
||||
@@ -235,7 +235,7 @@ async function initializeFalukantProducts() {
|
||||
{ labelTr: 'milk', category: 1, productionTime: 1, sellCost: 4 },
|
||||
{ labelTr: 'cheese', category: 1, productionTime: 1, sellCost: 4 },
|
||||
{ labelTr: 'bread', category: 1, productionTime: 1, sellCost: 4 },
|
||||
{ labelTr: 'beer', category: 1, productionTime: 1, sellCost: 4 },
|
||||
{ labelTr: 'beer', category: 2, productionTime: 3, sellCost: 4 },
|
||||
{ labelTr: 'iron', category: 2, productionTime: 4, sellCost: 15 },
|
||||
{ labelTr: 'copper', category: 2, productionTime: 4, sellCost: 15 },
|
||||
{ labelTr: 'spices', category: 2, productionTime: 8, sellCost: 30 },
|
||||
|
||||
Reference in New Issue
Block a user