11import type {
22 CustomLineItemReturnItem ,
3+ GeneralError ,
34 LineItemReturnItem ,
45 Order ,
56 OrderAddPaymentAction ,
@@ -9,10 +10,13 @@ import type {
910 OrderChangeShipmentStateAction ,
1011 OrderSetBillingAddressAction ,
1112 OrderSetCustomFieldAction ,
13+ OrderSetCustomLineItemCustomTypeAction ,
1214 OrderSetCustomTypeAction ,
1315 OrderSetCustomerEmailAction ,
1416 OrderSetCustomerIdAction ,
1517 OrderSetDeliveryCustomFieldAction ,
18+ OrderSetLineItemCustomFieldAction ,
19+ OrderSetLineItemCustomTypeAction ,
1620 OrderSetLocaleAction ,
1721 OrderSetOrderNumberAction ,
1822 OrderSetParcelCustomFieldAction ,
@@ -27,6 +31,7 @@ import type {
2731 Store ,
2832 SyncInfo ,
2933} from "@commercetools/platform-sdk" ;
34+ import { CommercetoolsError } from "~src/exceptions" ;
3035import { getBaseResourceProperties } from "~src/helpers" ;
3136import type { Writable } from "~src/types" ;
3237import type { RepositoryContext , UpdateHandlerInterface } from "../abstract" ;
@@ -205,6 +210,82 @@ export class OrderUpdateHandler
205210 }
206211 }
207212
213+ setLineItemCustomField (
214+ context : RepositoryContext ,
215+ resource : Order ,
216+ {
217+ lineItemId,
218+ lineItemKey,
219+ name,
220+ value,
221+ action,
222+ } : OrderSetLineItemCustomFieldAction ,
223+ ) {
224+ const lineItem = resource . lineItems . find (
225+ ( x ) =>
226+ ( lineItemId && x . id === lineItemId ) ||
227+ ( lineItemKey && x . key === lineItemKey ) ,
228+ ) ;
229+
230+ if ( ! lineItem ) {
231+ // Check if line item is found
232+ throw new CommercetoolsError < GeneralError > ( {
233+ code : "General" ,
234+ message : lineItemKey
235+ ? `A line item with key '${ lineItemKey } ' not found.`
236+ : `A line item with ID '${ lineItemId } ' not found.` ,
237+ } ) ;
238+ }
239+
240+ if ( ! lineItem . custom ) {
241+ throw new Error ( "Resource has no custom field" ) ;
242+ }
243+
244+ lineItem . custom . fields [ name ] = value ;
245+ }
246+
247+ setLineItemCustomType (
248+ context : RepositoryContext ,
249+ resource : Writable < Order > ,
250+ { lineItemId, lineItemKey, type, fields } : OrderSetLineItemCustomTypeAction ,
251+ ) {
252+ const lineItem = resource . lineItems . find (
253+ ( x ) =>
254+ ( lineItemId && x . id === lineItemId ) ||
255+ ( lineItemKey && x . key === lineItemKey ) ,
256+ ) ;
257+
258+ if ( ! lineItem ) {
259+ // Check if line item is found
260+ throw new CommercetoolsError < GeneralError > ( {
261+ code : "General" ,
262+ message : lineItemKey
263+ ? `A line item with key '${ lineItemKey } ' not found.`
264+ : `A line item with ID '${ lineItemId } ' not found.` ,
265+ } ) ;
266+ }
267+
268+ if ( ! type ) {
269+ lineItem . custom = undefined ;
270+ } else {
271+ const resolvedType = this . _storage . getByResourceIdentifier (
272+ context . projectKey ,
273+ type ,
274+ ) ;
275+ if ( ! resolvedType ) {
276+ throw new Error ( `Type ${ type } not found` ) ;
277+ }
278+
279+ lineItem . custom = {
280+ type : {
281+ typeId : "type" ,
282+ id : resolvedType . id ,
283+ } ,
284+ fields : fields || { } ,
285+ } ;
286+ }
287+ }
288+
208289 setLocale (
209290 context : RepositoryContext ,
210291 resource : Writable < Order > ,
0 commit comments