Füge neue Modelle für Produktion, Inventar und kaufbare Bestände hinzu; aktualisiere bestehende Modelle und Routen
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user