feat: managing returns
This commit is contained in:
parent
ca1eeafd8f
commit
6947bcfb01
11 changed files with 521 additions and 22 deletions
|
|
@ -1,5 +1,8 @@
|
|||
import 'package:seshat/data/services/api_client.dart';
|
||||
import 'package:seshat/domain/models/accounting.dart';
|
||||
import 'package:seshat/domain/models/bal.dart';
|
||||
import 'package:seshat/domain/models/bal_stats.dart';
|
||||
import 'package:seshat/domain/models/enums.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
class BalRepository {
|
||||
|
|
@ -7,6 +10,7 @@ class BalRepository {
|
|||
|
||||
final ApiClient _apiClient;
|
||||
List<Bal>? _bals;
|
||||
Accounting? accounting;
|
||||
|
||||
Future<Result<List<Bal>>> getBals() async {
|
||||
if (_bals != null) {
|
||||
|
|
@ -95,4 +99,71 @@ class BalRepository {
|
|||
await _getBalsNoCache();
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<Result<BalStats>> getBalStats(int id) async {
|
||||
return _apiClient.getBalStats(id);
|
||||
}
|
||||
|
||||
Future<Result<Accounting>> getAccountingNoCache(int balId) async {
|
||||
final result = await _apiClient.getAccounting(balId);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
accounting = result.value;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<Result<Accounting>> getAccounting(int balId) async {
|
||||
if (accounting != null) {
|
||||
return Result.ok(accounting!);
|
||||
}
|
||||
final result = await _apiClient.getAccounting(balId);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
accounting = result.value;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<Result<void>> returnToId(
|
||||
int balId,
|
||||
int ownerId,
|
||||
ReturnType type,
|
||||
) async {
|
||||
final result = await _apiClient.returnToId(balId, ownerId, type.name);
|
||||
switch (result) {
|
||||
case Ok():
|
||||
switch (type) {
|
||||
case ReturnType.books:
|
||||
final owner = accounting?.owners
|
||||
.where((el) => el.ownerId == ownerId)
|
||||
.firstOrNull;
|
||||
if (owner?.owedMoney == 0) {
|
||||
accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
|
||||
}
|
||||
owner?.owed = [];
|
||||
owner?.owedInstances = [];
|
||||
break;
|
||||
case ReturnType.money:
|
||||
final owner = accounting?.owners
|
||||
.where((el) => el.ownerId == ownerId)
|
||||
.firstOrNull;
|
||||
if (owner?.owed == null || owner!.owed.isEmpty) {
|
||||
accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
|
||||
}
|
||||
owner?.owedMoney = 0;
|
||||
break;
|
||||
case ReturnType.all:
|
||||
accounting?.owners.removeWhere((el) => el.ownerId == ownerId);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,14 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:seshat/config/constants.dart';
|
||||
import 'package:seshat/domain/models/accounting.dart';
|
||||
import 'package:seshat/domain/models/bal.dart';
|
||||
import 'package:seshat/domain/models/bal_stats.dart';
|
||||
import 'package:seshat/domain/models/book.dart';
|
||||
import 'package:seshat/domain/models/book_instance.dart';
|
||||
import 'package:seshat/domain/models/owner.dart';
|
||||
|
|
@ -12,6 +16,12 @@ import 'package:seshat/domain/models/search_result.dart';
|
|||
import 'package:seshat/utils/command.dart';
|
||||
import 'package:seshat/utils/result.dart';
|
||||
|
||||
extension StringExtension on String {
|
||||
String capitalize() {
|
||||
return "${this[0].toUpperCase()}${this.substring(1).toLowerCase()}";
|
||||
}
|
||||
}
|
||||
|
||||
typedef AuthHeaderProvider = String? Function();
|
||||
|
||||
class ApiClient {
|
||||
|
|
@ -37,12 +47,86 @@ class ApiClient {
|
|||
return headers;
|
||||
}
|
||||
|
||||
/*
|
||||
* ========================
|
||||
* =====< Accounting >=====
|
||||
* ========================
|
||||
*/
|
||||
|
||||
Future<Result<Accounting>> getAccounting(int balId) async {
|
||||
final client = Client();
|
||||
try {
|
||||
final headers = await _getHeaders();
|
||||
final response = await client.get(
|
||||
Uri.parse("https://$apiBasePath/bal/$balId/accounting"),
|
||||
headers: headers,
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
final json = jsonDecode(response.body);
|
||||
return Result.ok(Accounting.fromJSON(json));
|
||||
} else {
|
||||
throw "Unknown error";
|
||||
}
|
||||
} catch (e) {
|
||||
return Result.error(Exception(e));
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
|
||||
Future<Result<void>> returnToId(int balId, int ownerId, String type) async {
|
||||
final client = Client();
|
||||
try {
|
||||
final headers = await _getHeaders({"Content-Type": "application/json"});
|
||||
final body = jsonEncode({"return_type": type.capitalize()});
|
||||
debugPrint(body);
|
||||
final response = await client.post(
|
||||
Uri.parse(
|
||||
"https://$apiBasePath/bal/${balId.toString()}/accounting/return/${ownerId.toString()}",
|
||||
),
|
||||
headers: headers,
|
||||
body: body,
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
return Result.ok(response);
|
||||
} else {
|
||||
throw "Unknown error ${response.statusCode.toString()}";
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint(e.toString());
|
||||
return Result.error(Exception(e));
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* =================
|
||||
* =====[ BAL ]=====
|
||||
* =================
|
||||
*/
|
||||
|
||||
Future<Result<BalStats>> getBalStats(int id) async {
|
||||
final client = Client();
|
||||
try {
|
||||
final headers = await _getHeaders();
|
||||
final response = await client.get(
|
||||
Uri.parse("https://$apiBasePath/bal/${id.toString()}/stats"),
|
||||
headers: headers,
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
final json = jsonDecode(response.body);
|
||||
return Result.ok(BalStats.fromJSON(json));
|
||||
} else {
|
||||
throw "Unknown error";
|
||||
}
|
||||
} catch (e) {
|
||||
return Result.error(Exception(e));
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
|
||||
Future<Result<Bal>> stopBal(int id) async {
|
||||
final client = Client();
|
||||
try {
|
||||
|
|
|
|||
Reference in a new issue