void
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:data_channel/data_channel.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
@@ -202,4 +203,37 @@ class OrderRemoteDataProvider {
|
||||
return DC.error(OrderFailure.serverError(e));
|
||||
}
|
||||
}
|
||||
|
||||
Future<DC<OrderFailure, Unit>> voidOrder({
|
||||
required String orderId,
|
||||
required String reason,
|
||||
String type = "ITEM", // TYPE: ALL, ITEM
|
||||
required List<OrderItemDto> orderItems,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _apiClient.post(
|
||||
'${ApiPath.orders}/void',
|
||||
data: {
|
||||
'order_id': orderId,
|
||||
'type': orderItems.isEmpty ? "ALL" : type,
|
||||
'reason': reason,
|
||||
"items": orderItems
|
||||
.map(
|
||||
(item) => {'order_item_id': item.id, "quantity": item.quantity},
|
||||
)
|
||||
.toList(),
|
||||
},
|
||||
headers: getAuthorizationHeader(),
|
||||
);
|
||||
|
||||
if (response.data['success'] == false) {
|
||||
return DC.error(OrderFailure.unexpectedError());
|
||||
}
|
||||
|
||||
return DC.data(unit);
|
||||
} on ApiFailure catch (e, s) {
|
||||
log('voidOrderError', name: _logName, error: e, stackTrace: s);
|
||||
return DC.error(OrderFailure.serverError(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,4 +136,23 @@ class OrderItemDto with _$OrderItemDto {
|
||||
printerType: printerType ?? '',
|
||||
paidQuantity: paidQuantity ?? 0,
|
||||
);
|
||||
|
||||
factory OrderItemDto.fromDomain(OrderItem orderItem) => OrderItemDto(
|
||||
id: orderItem.id,
|
||||
orderId: orderItem.orderId,
|
||||
productId: orderItem.productId,
|
||||
productName: orderItem.productName,
|
||||
productVariantId: orderItem.productVariantId,
|
||||
productVariantName: orderItem.productVariantName,
|
||||
quantity: orderItem.quantity,
|
||||
unitPrice: orderItem.unitPrice,
|
||||
totalPrice: orderItem.totalPrice,
|
||||
modifiers: orderItem.modifiers,
|
||||
notes: orderItem.notes,
|
||||
status: orderItem.status,
|
||||
createdAt: orderItem.createdAt.toIso8601String(),
|
||||
updatedAt: orderItem.updatedAt.toIso8601String(),
|
||||
printerType: orderItem.printerType,
|
||||
paidQuantity: orderItem.paidQuantity,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -98,11 +98,18 @@ class OrderItemRequestDto with _$OrderItemRequestDto {
|
||||
notes: request.notes,
|
||||
);
|
||||
|
||||
Map<String, dynamic> toRequest() => {
|
||||
"product_id": productId,
|
||||
"product_variant_id": productVariantId,
|
||||
"quantity": quantity,
|
||||
"unit_price": unitPrice,
|
||||
"notes": notes,
|
||||
};
|
||||
Map<String, dynamic> toRequest() {
|
||||
Map<String, dynamic> data = {
|
||||
"product_id": productId,
|
||||
"quantity": quantity,
|
||||
"unit_price": unitPrice,
|
||||
"notes": notes,
|
||||
};
|
||||
|
||||
if (productVariantId != null && productVariantId != '') {
|
||||
data["product_variant_id"] = productVariantId;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,4 +151,30 @@ class OrderRepository implements IOrderRepository {
|
||||
return left(const OrderFailure.unexpectedError());
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<OrderFailure, Unit>> voidOrder({
|
||||
required String orderId,
|
||||
required String reason,
|
||||
String type = "ITEM",
|
||||
required List<OrderItem> orderItems,
|
||||
}) async {
|
||||
try {
|
||||
final result = await _dataProvider.voidOrder(
|
||||
orderId: orderId,
|
||||
reason: reason,
|
||||
type: type,
|
||||
orderItems: orderItems.map((e) => OrderItemDto.fromDomain(e)).toList(),
|
||||
);
|
||||
|
||||
if (result.hasError) {
|
||||
return left(result.error!);
|
||||
}
|
||||
|
||||
return right(unit);
|
||||
} catch (e) {
|
||||
log('voidOrderError', name: _logName, error: e);
|
||||
return left(const OrderFailure.unexpectedError());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user