-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ReceiptsRepository now use v2 endpoint, * User can purchase free products in the app, when such products occur in the database
- Loading branch information
Showing
21 changed files
with
253 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import 'package:coffeecard/core/errors/failures.dart'; | ||
import 'package:coffeecard/core/network/network_request_executor.dart'; | ||
import 'package:coffeecard/data/repositories/v1/product_repository.dart'; | ||
import 'package:coffeecard/generated/api/coffeecard_api_v2.swagger.dart'; | ||
import 'package:coffeecard/models/receipts/receipt.dart'; | ||
import 'package:dartz/dartz.dart'; | ||
|
||
class ReceiptRepository { | ||
ReceiptRepository({ | ||
required this.productRepository, | ||
required this.apiV2, | ||
required this.executor, | ||
}); | ||
|
||
final CoffeecardApiV2 apiV2; | ||
final ProductRepository productRepository; | ||
final NetworkRequestExecutor executor; | ||
|
||
/// Retrieves all of the users receipts | ||
/// This includes both their used tickets and purchased tickets | ||
Future<Either<Failure, List<Receipt>>> getUserReceipts() async { | ||
final usedTicketsFutureEither = executor( | ||
() => apiV2.apiV2TicketsGet(includeUsed: true), | ||
); | ||
final purchasedTicketsFutureEither = executor( | ||
apiV2.apiV2PurchasesGet, | ||
); | ||
|
||
final usedTicketsEither = (await usedTicketsFutureEither) | ||
.map((dto) => dto.map(Receipt.fromTicketResponse)); | ||
final purchasedTicketsEither = (await purchasedTicketsFutureEither).map( | ||
(r) => r.map( | ||
(purchase) => Receipt.fromSimplePurchaseResponse( | ||
purchase, | ||
), | ||
), | ||
); | ||
|
||
return usedTicketsEither.fold( | ||
(l) => Left(l), | ||
(usedTickets) => purchasedTicketsEither.map( | ||
(purchasedTickets) { | ||
final allTickets = [...usedTickets, ...purchasedTickets]; | ||
allTickets.sort((a, b) => b.timeUsed.compareTo(a.timeUsed)); | ||
return allTickets; | ||
}, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
import 'package:coffeecard/generated/api/coffeecard_api_v2.swagger.dart'; | ||
import 'package:coffeecard/models/purchase/payment_status.dart'; | ||
|
||
class SinglePurchase { | ||
final int id; | ||
final int totalAmount; | ||
final Map<String, dynamic> paymentDetails; | ||
final String purchaseStatus; | ||
final PaymentStatus status; | ||
final DateTime dateCreated; | ||
|
||
const SinglePurchase({ | ||
required this.id, | ||
required this.totalAmount, | ||
required this.paymentDetails, | ||
required this.purchaseStatus, | ||
required this.status, | ||
required this.dateCreated, | ||
}); | ||
|
||
SinglePurchase.fromDto(SinglePurchaseResponse dto) | ||
: id = dto.id, | ||
totalAmount = dto.totalAmount, | ||
paymentDetails = dto.paymentDetails as Map<String, dynamic>, | ||
purchaseStatus = dto.purchaseStatus as String, | ||
status = PaymentStatus.fromPurchaseStatus( | ||
purchaseStatusFromJson(dto.purchaseStatus), | ||
), | ||
dateCreated = dto.dateCreated; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import 'package:coffeecard/core/errors/failures.dart'; | ||
import 'package:coffeecard/generated/api/coffeecard_api_v2.enums.swagger.dart'; | ||
import 'package:coffeecard/models/purchase/payment.dart'; | ||
import 'package:coffeecard/models/purchase/payment_status.dart'; | ||
import 'package:coffeecard/payment/payment_handler.dart'; | ||
import 'package:dartz/dartz.dart'; | ||
|
||
class FreeProductService extends PaymentHandler { | ||
const FreeProductService({ | ||
required super.purchaseRepository, | ||
required super.context, | ||
}); | ||
|
||
@override | ||
Future<Either<Failure, Payment>> initPurchase(int productId) async { | ||
final response = await purchaseRepository.initiatePurchase( | ||
productId, | ||
PaymentType.freepurchase, | ||
); | ||
|
||
return response.fold( | ||
(error) => Left(error), | ||
(purchase) => Right( | ||
Payment( | ||
id: purchase.id, | ||
status: PaymentStatus.completed, | ||
deeplink: '', | ||
purchaseTime: purchase.dateCreated, | ||
price: purchase.totalAmount, | ||
productId: purchase.productId, | ||
productName: purchase.productName, | ||
), | ||
), | ||
); | ||
} | ||
} |
Oops, something went wrong.