@@ -681,6 +681,101 @@ module.exports = (sequelize, DataTypes) => {
681681 return false ;
682682 }
683683
684+ /**
685+ * @param {Object } payload
686+ * @param {Object } product
687+ */
688+ static async getUpdateProductError ( payload , product ) {
689+ if ( ! payload . name ) {
690+ return "The name field is required." ;
691+ } else if ( "string" !== typeof payload . name ) {
692+ return "The name field must be a string." ;
693+ } else if ( 50 < payload . name . length ) {
694+ return "The name field must be less than 51 characters." ;
695+ } else {
696+ const newSlug = slugify ( payload . name ) ;
697+ if ( newSlug !== product . slug ) {
698+ const nameExists = await this . getProductBySlug (
699+ newSlug ,
700+ ) ;
701+ if ( false !== nameExists ) {
702+ return "The name field is taken." ;
703+ }
704+ }
705+ }
706+
707+ if ( undefined === payload . units ) {
708+ return "The units field is required." ;
709+ } else if ( null === `${ payload . units } ` . match ( integerNumberRegex ) ) {
710+ return "The units field must be a number"
711+ }
712+
713+ if ( undefined === payload . weight ) {
714+ return "The weight field is required." ;
715+ } else if ( null === `${ payload . weight } ` . match ( numberWithOptionalDecimalPartRegex ) ) {
716+ return "The weight field must be a number"
717+ }
718+
719+ if ( undefined === payload . price ) {
720+ return "The price field is required." ;
721+ } else if ( null === `${ payload . price } ` . match ( numberWithOptionalDecimalPartRegex ) ) {
722+ return "The price field must be a number"
723+ }
724+
725+ if ( payload . description ) {
726+ if ( "string" !== typeof payload . description ) {
727+ return "The description field must be a string." ;
728+ } else if ( 1000 < payload . description . length ) {
729+ return "The description field must be less than 1001 characters." ;
730+ }
731+ }
732+
733+ if ( payload . category && Number ( payload . category ) ) {
734+ if ( null === `${ payload . category } ` . match ( integerNumberRegex ) ) {
735+ return "The category field must be a valid integer." ;
736+ }
737+
738+ const foundCategory = await this . sequelize
739+ . models
740+ . category
741+ . getProductCategory (
742+ payload . category
743+ ) ;
744+ if ( false === foundCategory ) {
745+ return "The category field was not found in our records." ;
746+ }
747+ }
748+
749+ if ( payload . manufacturer && Number ( payload . manufacturer ) ) {
750+ if ( null === `${ payload . manufacturer } ` . match ( integerNumberRegex ) ) {
751+ return "The manufacturer field must be a valid integer." ;
752+ }
753+
754+ const foundManufacturer = await this . sequelize
755+ . models
756+ . manufacturer
757+ . getProductManufacturer (
758+ payload . manufacturer
759+ ) ;
760+ if ( false === foundManufacturer ) {
761+ return "The manufacturer field was not found in our records." ;
762+ }
763+ }
764+
765+ if ( undefined === typeof payload . isLive ) {
766+ return "The isLive field is missing." ;
767+ } else if (
768+ false !== payload . isLive &&
769+ true !== payload . isLive &&
770+ "false" !== payload . isLive &&
771+ "true" !== payload . isLive
772+ ) {
773+ return "The isLive field must be true or false." ;
774+ }
775+
776+ return false ;
777+ }
778+
684779 static getNewProductData ( payload ) {
685780 const result = {
686781 name : payload . name ,
@@ -706,6 +801,31 @@ module.exports = (sequelize, DataTypes) => {
706801 return result ;
707802 }
708803
804+ static getEditProductData ( payload ) {
805+ const result = {
806+ name : payload . name ,
807+ units : payload . units ,
808+ weight : payload . weight ,
809+ price : payload . price ,
810+ isLive : false ,
811+ } ;
812+ if ( payload . description ) {
813+ result . description = payload . description ;
814+ }
815+ if ( payload . category ) {
816+ result . category = payload . category ;
817+ }
818+ if ( payload . manufacturer ) {
819+ result . manufacturer = payload . manufacturer ;
820+ }
821+ if ( undefined !== payload . isLive ) {
822+ if ( true === payload . isLive || "true" === payload . isLive ) {
823+ result . isLive = true ;
824+ }
825+ }
826+ return result ;
827+ }
828+
709829 /**
710830 * @param {Object } data
711831 * @returns {object|false }
@@ -745,6 +865,55 @@ module.exports = (sequelize, DataTypes) => {
745865 return false ;
746866 }
747867 }
868+
869+ /**
870+ * @param {number } id
871+ * @param {Object } data
872+ * @returns {object|false }
873+ */
874+ static async updateProduct ( id , data ) {
875+ try {
876+ const result = await sequelize . query (
877+ `UPDATE ${ this . getTableName ( ) }
878+ SET name = :name,
879+ slug = :slug,
880+ units = :units,
881+ weight = :weight,
882+ categoriesId = :categoriesId,
883+ price = :price,
884+ description = :description,
885+ manufacturersId = :manufacturersId,
886+ isLive = :isLive,
887+ updatedAt = :updatedAt
888+ WHERE id = :id` ,
889+ {
890+ type : sequelize . QueryTypes . UPDATE ,
891+ replacements : {
892+ slug : slugify ( data . name ) ,
893+ name : data . name ,
894+ units : data . units ,
895+ weight : data . weight + " kg" ,
896+ price : data . price ,
897+ description : data . description || null ,
898+ categoriesId : data . category || null ,
899+ manufacturersId : data . manufacturer || null ,
900+ isLive : data . isLive ,
901+ updatedAt : moment ( )
902+ . utc ( )
903+ . format ( mysqlTimeFormat ) ,
904+ id,
905+ } ,
906+ } ,
907+ ) ;
908+
909+ return { productId : result [ 0 ] } ;
910+ } catch ( err ) {
911+ if ( "production" !== nodeEnv ) {
912+ console . log ( err ) ;
913+ }
914+ return false ;
915+ }
916+ }
748917 }
749918 product . init ( {
750919 id : {
0 commit comments