151 lines
4.6 KiB
Dart
151 lines
4.6 KiB
Dart
// Dart imports:
|
|
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:core';
|
|
|
|
// Flutter imports:
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
// Package imports:
|
|
import 'package:built_collection/built_collection.dart';
|
|
import 'package:http/http.dart';
|
|
|
|
// Project imports:
|
|
import 'package:invoiceninja_flutter/constants.dart';
|
|
import 'package:invoiceninja_flutter/data/models/models.dart';
|
|
import 'package:invoiceninja_flutter/data/models/serializers.dart';
|
|
import 'package:invoiceninja_flutter/data/web_client.dart';
|
|
import 'package:invoiceninja_flutter/redux/app/app_state.dart';
|
|
import 'package:invoiceninja_flutter/utils/serialization.dart';
|
|
|
|
class InvoiceRepository {
|
|
const InvoiceRepository({
|
|
this.webClient = const WebClient(),
|
|
});
|
|
|
|
final WebClient webClient;
|
|
|
|
Future<InvoiceEntity> loadItem(
|
|
Credentials credentials, String entityId) async {
|
|
final dynamic response = await webClient.get(
|
|
'${credentials.url}/invoices/$entityId?include=activities.history',
|
|
credentials.token);
|
|
|
|
final InvoiceItemResponse invoiceResponse = await compute<dynamic, dynamic>(
|
|
SerializationUtils.computeDecode,
|
|
<dynamic>[InvoiceItemResponse.serializer, response]);
|
|
|
|
return invoiceResponse.data;
|
|
}
|
|
|
|
Future<BuiltList<InvoiceEntity>> loadList(
|
|
Credentials credentials, int createdAt, bool filterDeleted) async {
|
|
String url = credentials.url + '/invoices?created_at=$createdAt';
|
|
|
|
if (filterDeleted) {
|
|
url += '&filter_deleted_clients=true';
|
|
}
|
|
|
|
final dynamic response = await webClient.get(url, credentials.token);
|
|
|
|
final InvoiceListResponse invoiceResponse = await compute<dynamic, dynamic>(
|
|
SerializationUtils.computeDecode,
|
|
<dynamic>[InvoiceListResponse.serializer, response]);
|
|
|
|
return invoiceResponse.data;
|
|
}
|
|
|
|
Future<List<InvoiceEntity>> bulkAction(
|
|
Credentials credentials, List<String> ids, EntityAction action) async {
|
|
if (ids.length > kMaxEntitiesPerBulkAction) {
|
|
ids = ids.sublist(0, kMaxEntitiesPerBulkAction);
|
|
}
|
|
|
|
final url = credentials.url + '/invoices/bulk';
|
|
final dynamic response = await webClient.post(url, credentials.token,
|
|
data: json.encode({'ids': ids, 'action': action.toApiParam()}));
|
|
|
|
final InvoiceListResponse invoiceResponse =
|
|
serializers.deserializeWith(InvoiceListResponse.serializer, response);
|
|
|
|
return invoiceResponse.data.toList();
|
|
}
|
|
|
|
Future<InvoiceEntity> saveData(
|
|
Credentials credentials,
|
|
InvoiceEntity invoice, {
|
|
EntityAction action,
|
|
}) async {
|
|
invoice = invoice.rebuild((b) => b..documents.clear());
|
|
final data = serializers.serializeWith(InvoiceEntity.serializer, invoice);
|
|
dynamic response;
|
|
String url;
|
|
|
|
if (invoice.isNew) {
|
|
url = credentials.url + '/invoices?include=activities.history';
|
|
} else {
|
|
url =
|
|
'${credentials.url}/invoices/${invoice.id}?include=activities.history';
|
|
}
|
|
|
|
if (action == EntityAction.markPaid) {
|
|
url += '&paid=true';
|
|
} else if (action == EntityAction.markSent) {
|
|
url += '&mark_sent=true';
|
|
}
|
|
|
|
if (invoice.isNew) {
|
|
response =
|
|
await webClient.post(url, credentials.token, data: json.encode(data));
|
|
} else {
|
|
response =
|
|
await webClient.put(url, credentials.token, data: json.encode(data));
|
|
}
|
|
|
|
final InvoiceItemResponse invoiceResponse =
|
|
serializers.deserializeWith(InvoiceItemResponse.serializer, response);
|
|
|
|
return invoiceResponse.data;
|
|
}
|
|
|
|
Future<InvoiceEntity> emailInvoice(
|
|
Credentials credentials,
|
|
InvoiceEntity invoice,
|
|
EmailTemplate template,
|
|
String subject,
|
|
String body) async {
|
|
final data = {
|
|
'entity': '${invoice.entityType}',
|
|
'entity_id': invoice.id,
|
|
'template': 'email_template_$template',
|
|
'body': body,
|
|
'subject': subject,
|
|
};
|
|
|
|
final dynamic response = await webClient.post(
|
|
credentials.url + '/emails', credentials.token,
|
|
data: json.encode(data));
|
|
|
|
final InvoiceItemResponse invoiceResponse =
|
|
serializers.deserializeWith(InvoiceItemResponse.serializer, response);
|
|
|
|
return invoiceResponse.data;
|
|
}
|
|
|
|
Future<InvoiceEntity> uploadDocument(Credentials credentials,
|
|
BaseEntity entity, MultipartFile multipartFile) async {
|
|
final fields = <String, String>{
|
|
'_method': 'put',
|
|
};
|
|
|
|
final dynamic response = await webClient.post(
|
|
'${credentials.url}/invoices/${entity.id}/upload', credentials.token,
|
|
data: fields, multipartFiles: [multipartFile]);
|
|
|
|
final InvoiceItemResponse invoiceResponse =
|
|
serializers.deserializeWith(InvoiceItemResponse.serializer, response);
|
|
|
|
return invoiceResponse.data;
|
|
}
|
|
}
|