2024-11-11 01:11:24 +03:00
import ProductService from './product.js' ;
2024-10-26 20:18:14 +03:00
import db from '../db.js' ;
2024-10-27 04:45:13 +03:00
import errorHandler from '../utils/pgerrorhandler.js' ;
2024-10-31 06:33:57 +03:00
import responses from '../response/responseCodes.js' ;
import customError from '../response/customError.js' ;
2024-10-26 20:18:14 +03:00
class AbstractProductService {
2024-10-27 04:45:13 +03:00
async create ( groupid , localid , barcode , name , net _weight , image _filename , category , unit ) {
await db . query ( "INSERT INTO abstract_products (group_id, local_id, barcode, name, net_weight, image_filename, category, unit) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)" , [ groupid , localid , barcode , name , net _weight , image _filename , category , unit ] )
. catch ( ( e ) => {
2024-10-31 06:33:57 +03:00
errorHandler ( e , "abstractproduct" )
2024-10-27 04:45:13 +03:00
} ) ;
}
async updateBarcode ( groupId , localId , barcode ) {
await db . query ( "UPDATE abstract_products SET barcode = $1 WHERE group_id = $2 AND local_id = $3" , [ barcode , groupId , localId ] )
. catch ( ( e ) => {
errorHandler ( e , "barcode" )
} ) ;
}
async updateName ( groupId , localId , name ) {
await db . query ( "UPDATE abstract_products SET name = $1 WHERE group_id = $2 AND local_id = $3" , [ name , groupId , localId ] )
. catch ( ( e ) => {
2024-10-31 06:33:57 +03:00
errorHandler ( e , "abstractproductname" )
2024-10-27 04:45:13 +03:00
} ) ;
}
async updateNetWeight ( groupId , localId , net _weight ) {
2024-11-06 03:48:45 +03:00
await db . query ( "UPDATE abstract_products SET net_weight = $1 WHERE group_id = $2 AND local_id = $3" , [ net _weight , groupId , localId ] )
2024-10-27 04:45:13 +03:00
. catch ( ( e ) => {
2024-10-31 06:33:57 +03:00
errorHandler ( e , "netweight" )
2024-11-06 03:48:45 +03:00
} ) ;
2024-10-27 04:45:13 +03:00
}
async updateImageFilename ( groupId , localId , image _filename ) {
await db . query ( "UPDATE abstract_products SET image_filename = $1 WHERE group_id = $2 AND local_id = $3" , [ image _filename , groupId , localId ] )
. catch ( ( e ) => {
2024-10-31 06:33:57 +03:00
errorHandler ( e , "imagehash" )
2024-10-27 04:45:13 +03:00
} ) ;
}
async updateCategory ( groupId , localId , category ) {
await db . query ( "UPDATE abstract_products SET category = $1 WHERE group_id = $2 AND local_id = $3" , [ category , groupId , localId ] )
. catch ( ( e ) => {
errorHandler ( e , "category" )
} ) ;
}
async updateUnit ( groupId , localId , unit ) {
await db . query ( "UPDATE abstract_products SET unit = $1 WHERE group_id = $2 AND local_id = $3" , [ unit , groupId , localId ] )
. catch ( ( e ) => {
errorHandler ( e , "unit" )
} ) ;
}
async getAll ( groupId ) {
2024-10-27 05:45:12 +03:00
let result = ( await db . query ( "SELECT local_id, barcode, name, net_weight, image_filename, category, unit FROM abstract_products WHERE group_id = $1" , [ groupId ] ) ) . rows ;
2024-11-07 03:11:07 +03:00
// if (!result) throw new customError("getAll abstract products not found", responses.responses.abstractproducts.not_found)
2024-10-27 05:45:12 +03:00
return result ;
2024-10-27 04:45:13 +03:00
}
async exists ( groupId , localId ) {
let result = ( await db . query ( "SELECT * FROM abstract_products WHERE group_id = $1 AND local_id = $2" , [ groupId , localId ] ) ) . rowCount ;
if ( ! result ) return false ;
return true ;
2024-10-26 20:18:14 +03:00
}
2024-11-07 21:44:10 +03:00
async getByLocalId ( groupId , localId ) {
let result = ( await db . query ( "SELECT * FROM abstract_products WHERE group_id = $1 AND local_id = $2" , [ groupId , localId ] ) )
if ( result . rowCount == 0 ) throw new customError ( ` Abstract product not found ` , responses . responses . abstractproducts . not _found )
return result . rows [ 0 ]
}
2024-11-11 01:11:24 +03:00
async getAllFromCategory ( groupId , categoryId ) {
let result = ( await db . query ( "SELECT * FROM abstract_products WHERE group_id = $1 AND category = $2" , [ groupId , categoryId ] ) ) ;
return result . rows
}
async delete ( groupId , localId ) {
let productsOfAbstractProduct = await ProductService . getByAbstractProductID ( groupId , localId ) ;
productsOfAbstractProduct . forEach ( async ( product ) => {
await ProductService . delete ( product . id )
} ) ;
await db . query ( "DELETE FROM abstract_products WHERE group_id = $1 AND local_id = $2" , [ groupId , localId ] )
}
2024-10-26 20:18:14 +03:00
} ;
export default new AbstractProductService ( ) ;