narinder1231's picture
fix vendor retrieval logic in isVendorHasDefaultBillsplitAccount function
b077308
import { Request, Response } from 'express';
import { logger } from '../../utils/logger';
import { syncVendorsFromJsonService } from '../../shared/services/vendors.service';
import PWVendors from '../../models/pwVendors';
import PWGlaccounts from '../../models/pwGlaccounts';
export const fetchVendorsData = async (req: Request, res: Response) => {
try {
const vendors = await PWVendors.findAll({
attributes: [['pw_id' , 'id'], 'name'],
order: [['name', 'ASC']],
});
const vendorsResponseData = vendors.map((vendor) => ({
id: vendor.id,
name: vendor.name,
}));
res.status(200).json(vendorsResponseData);
} catch (error) {
logger.error('Error fetching Vendors');
logger.error(error);
res.status(500).json({ error: 'Error fetching Vendors' });
}
};
export const isVendorHasDefaultBillsplitAccount = async (vendorId: number) => {
try {
const vendor = await PWVendors.findOne({ where: { pw_id: vendorId }, attributes: ['default_bill_split'] });
if (vendor && vendor.default_bill_split) {
const accNumber = vendor.default_bill_split.toString();
const GLaccount = await PWGlaccounts.findOne({ where: { account_number: accNumber } });
if (GLaccount)
return GLaccount.pw_id;
}
return null;
} catch (error) {
logger.error('Error checking default bill split account:', error);
return null;
}
};
export const syncVendors = async (req: Request, res: Response) => {
try {
const result = await syncVendorsFromJsonService();
res.status(200).send(result);
} catch (error) {
logger.error('Error syncing Vendors');
logger.error(error);
res.status(500).send((error as Error).message);
}
};