Bulk email

This commit is contained in:
Hillel Coren 2020-08-13 22:59:55 +03:00
parent c3815eda9b
commit d70f686131
22 changed files with 59 additions and 22 deletions

View File

@ -86,6 +86,16 @@ class EntityAction extends EnumClass {
return toSnakeCase(super.toString()); return toSnakeCase(super.toString());
} }
String toApiParam() {
final value = toString();
if (value.startsWith('email')) {
return 'email';
}
return value;
}
static BuiltSet<EntityAction> get values => _$values; static BuiltSet<EntityAction> get values => _$values;
static EntityAction valueOf(String name) => _$valueOf(name); static EntityAction valueOf(String name) => _$valueOf(name);

View File

@ -45,7 +45,7 @@ class ClientRepository {
final url = final url =
credentials.url + '/clients/bulk?include=gateway_tokens,activities'; credentials.url + '/clients/bulk?include=gateway_tokens,activities';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final ClientListResponse clientResponse = final ClientListResponse clientResponse =
serializers.deserializeWith(ClientListResponse.serializer, response); serializers.deserializeWith(ClientListResponse.serializer, response);
@ -87,7 +87,7 @@ class ClientRepository {
data: fields, filePath: filePath, fileIndex: 'documents[]'); data: fields, filePath: filePath, fileIndex: 'documents[]');
final ClientItemResponse clientResponse = final ClientItemResponse clientResponse =
serializers.deserializeWith(ClientItemResponse.serializer, response); serializers.deserializeWith(ClientItemResponse.serializer, response);
return clientResponse.data; return clientResponse.data;
} }

View File

@ -43,7 +43,7 @@ class CompanyGatewayRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/company_gateways/bulk?include=gateway'; final url = credentials.url + '/company_gateways/bulk?include=gateway';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final CompanyGatewayListResponse companyGatewayResponse = serializers final CompanyGatewayListResponse companyGatewayResponse = serializers
.deserializeWith(CompanyGatewayListResponse.serializer, response); .deserializeWith(CompanyGatewayListResponse.serializer, response);

View File

@ -41,7 +41,7 @@ class CreditRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/credits/bulk'; final url = credentials.url + '/credits/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final InvoiceListResponse invoiceResponse = final InvoiceListResponse invoiceResponse =
serializers.deserializeWith(InvoiceListResponse.serializer, response); serializers.deserializeWith(InvoiceListResponse.serializer, response);

View File

@ -40,7 +40,7 @@ class DesignRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/designs/bulk'; final url = credentials.url + '/designs/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final DesignListResponse designResponse = final DesignListResponse designResponse =
serializers.deserializeWith(DesignListResponse.serializer, response); serializers.deserializeWith(DesignListResponse.serializer, response);

View File

@ -40,7 +40,7 @@ class DocumentRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/documents/bulk'; final url = credentials.url + '/documents/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final DocumentListResponse documentResponse = final DocumentListResponse documentResponse =
serializers.deserializeWith(DocumentListResponse.serializer, response); serializers.deserializeWith(DocumentListResponse.serializer, response);

View File

@ -42,7 +42,7 @@ class ExpenseRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/expenses/bulk'; final url = credentials.url + '/expenses/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final ExpenseListResponse expenseResponse = final ExpenseListResponse expenseResponse =
serializers.deserializeWith(ExpenseListResponse.serializer, response); serializers.deserializeWith(ExpenseListResponse.serializer, response);

View File

@ -40,7 +40,7 @@ class GroupRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/group_settings/bulk'; final url = credentials.url + '/group_settings/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final GroupListResponse groupResponse = final GroupListResponse groupResponse =
serializers.deserializeWith(GroupListResponse.serializer, response); serializers.deserializeWith(GroupListResponse.serializer, response);

View File

@ -19,7 +19,8 @@ class InvoiceRepository {
Future<InvoiceEntity> loadItem( Future<InvoiceEntity> loadItem(
Credentials credentials, String entityId) async { Credentials credentials, String entityId) async {
final dynamic response = await webClient.get( final dynamic response = await webClient.get(
'${credentials.url}/invoices/$entityId?include=history', credentials.token); '${credentials.url}/invoices/$entityId?include=history',
credentials.token);
final InvoiceItemResponse invoiceResponse = await compute<dynamic, dynamic>( final InvoiceItemResponse invoiceResponse = await compute<dynamic, dynamic>(
computeDecode, <dynamic>[InvoiceItemResponse.serializer, response]); computeDecode, <dynamic>[InvoiceItemResponse.serializer, response]);
@ -42,7 +43,7 @@ class InvoiceRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/invoices/bulk'; final url = credentials.url + '/invoices/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final InvoiceListResponse invoiceResponse = final InvoiceListResponse invoiceResponse =
serializers.deserializeWith(InvoiceListResponse.serializer, response); serializers.deserializeWith(InvoiceListResponse.serializer, response);

View File

@ -43,7 +43,7 @@ class PaymentRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/payments/bulk?include=paymentables'; final url = credentials.url + '/payments/bulk?include=paymentables';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final PaymentListResponse paymentResponse = final PaymentListResponse paymentResponse =
serializers.deserializeWith(PaymentListResponse.serializer, response); serializers.deserializeWith(PaymentListResponse.serializer, response);

View File

@ -41,7 +41,7 @@ class PaymentTermRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/payment_terms/bulk'; final url = credentials.url + '/payment_terms/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final PaymentTermListResponse paymentTermResponse = serializers final PaymentTermListResponse paymentTermResponse = serializers
.deserializeWith(PaymentTermListResponse.serializer, response); .deserializeWith(PaymentTermListResponse.serializer, response);

View File

@ -44,7 +44,7 @@ class ProductRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/products/bulk'; final url = credentials.url + '/products/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final ProductListResponse productResponse = final ProductListResponse productResponse =
serializers.deserializeWith(ProductListResponse.serializer, response); serializers.deserializeWith(ProductListResponse.serializer, response);

View File

@ -42,7 +42,7 @@ class ProjectRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/projects/bulk'; final url = credentials.url + '/projects/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final ProjectListResponse projectResponse = final ProjectListResponse projectResponse =
serializers.deserializeWith(ProjectListResponse.serializer, response); serializers.deserializeWith(ProjectListResponse.serializer, response);

View File

@ -42,7 +42,7 @@ class QuoteRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/quotes/bulk'; final url = credentials.url + '/quotes/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final InvoiceListResponse invoiceResponse = final InvoiceListResponse invoiceResponse =
serializers.deserializeWith(InvoiceListResponse.serializer, response); serializers.deserializeWith(InvoiceListResponse.serializer, response);

View File

@ -41,7 +41,7 @@ class TaskRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/tasks/bulk'; final url = credentials.url + '/tasks/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final TaskListResponse taskResponse = final TaskListResponse taskResponse =
serializers.deserializeWith(TaskListResponse.serializer, response); serializers.deserializeWith(TaskListResponse.serializer, response);

View File

@ -40,7 +40,7 @@ class TaxRateRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/tax_rates/bulk'; final url = credentials.url + '/tax_rates/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final TaxRateListResponse taxRateResponse = final TaxRateListResponse taxRateResponse =
serializers.deserializeWith(TaxRateListResponse.serializer, response); serializers.deserializeWith(TaxRateListResponse.serializer, response);

View File

@ -39,7 +39,7 @@ class TokenRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/tokens/bulk'; final url = credentials.url + '/tokens/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final TokenListResponse tokenResponse = final TokenListResponse tokenResponse =
serializers.deserializeWith(TokenListResponse.serializer, response); serializers.deserializeWith(TokenListResponse.serializer, response);

View File

@ -40,7 +40,7 @@ class UserRepository {
EntityAction action, String password) async { EntityAction action, String password) async {
final url = credentials.url + '/users/bulk?include=company_user'; final url = credentials.url + '/users/bulk?include=company_user';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final UserListResponse userResponse = final UserListResponse userResponse =
serializers.deserializeWith(UserListResponse.serializer, response); serializers.deserializeWith(UserListResponse.serializer, response);

View File

@ -42,7 +42,7 @@ class VendorRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/vendors/bulk'; final url = credentials.url + '/vendors/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final VendorListResponse vendorResponse = final VendorListResponse vendorResponse =
serializers.deserializeWith(VendorListResponse.serializer, response); serializers.deserializeWith(VendorListResponse.serializer, response);

View File

@ -40,7 +40,7 @@ class WebhookRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/webhooks/bulk'; final url = credentials.url + '/webhooks/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final WebhookListResponse webhookResponse = final WebhookListResponse webhookResponse =
serializers.deserializeWith(WebhookListResponse.serializer, response); serializers.deserializeWith(WebhookListResponse.serializer, response);

View File

@ -28,6 +28,7 @@ List<Middleware<AppState>> createStoreInvoicesMiddleware([
final deleteInvoice = _deleteInvoice(repository); final deleteInvoice = _deleteInvoice(repository);
final restoreInvoice = _restoreInvoice(repository); final restoreInvoice = _restoreInvoice(repository);
final emailInvoice = _emailInvoice(repository); final emailInvoice = _emailInvoice(repository);
final bulkEmailInvoices = _bulkEmailInvoices(repository);
final markInvoiceSent = _markInvoiceSent(repository); final markInvoiceSent = _markInvoiceSent(repository);
final markInvoicePaid = _markInvoicePaid(repository); final markInvoicePaid = _markInvoicePaid(repository);
final reverseInvoices = _reverseInvoices(repository); final reverseInvoices = _reverseInvoices(repository);
@ -46,6 +47,7 @@ List<Middleware<AppState>> createStoreInvoicesMiddleware([
TypedMiddleware<AppState, DeleteInvoicesRequest>(deleteInvoice), TypedMiddleware<AppState, DeleteInvoicesRequest>(deleteInvoice),
TypedMiddleware<AppState, RestoreInvoicesRequest>(restoreInvoice), TypedMiddleware<AppState, RestoreInvoicesRequest>(restoreInvoice),
TypedMiddleware<AppState, EmailInvoiceRequest>(emailInvoice), TypedMiddleware<AppState, EmailInvoiceRequest>(emailInvoice),
TypedMiddleware<AppState, BulkEmailInvoicesRequest>(bulkEmailInvoices),
TypedMiddleware<AppState, MarkInvoicesSentRequest>(markInvoiceSent), TypedMiddleware<AppState, MarkInvoicesSentRequest>(markInvoiceSent),
TypedMiddleware<AppState, MarkInvoicesPaidRequest>(markInvoicePaid), TypedMiddleware<AppState, MarkInvoicesPaidRequest>(markInvoicePaid),
TypedMiddleware<AppState, ReverseInvoicesRequest>(reverseInvoices), TypedMiddleware<AppState, ReverseInvoicesRequest>(reverseInvoices),
@ -323,6 +325,30 @@ Middleware<AppState> _emailInvoice(InvoiceRepository repository) {
}; };
} }
Middleware<AppState> _bulkEmailInvoices(InvoiceRepository repository) {
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
final action = dynamicAction as BulkEmailInvoicesRequest;
repository
.bulkAction(store.state.credentials, action.invoiceIds,
EntityAction.emailInvoice)
.then((List<InvoiceEntity> invoices) {
store.dispatch(BulkEmailInvoicesSuccess(invoices));
if (action.completer != null) {
action.completer.complete(null);
}
}).catchError((Object error) {
print(error);
store.dispatch(BulkEmailInvoicesFailure(error));
if (action.completer != null) {
action.completer.completeError(error);
}
});
next(action);
};
}
Middleware<AppState> _saveInvoice(InvoiceRepository repository) { Middleware<AppState> _saveInvoice(InvoiceRepository repository) {
return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) { return (Store<AppState> store, dynamic dynamicAction, NextDispatcher next) {
final action = dynamicAction as SaveInvoiceRequest; final action = dynamicAction as SaveInvoiceRequest;

View File

@ -46,7 +46,7 @@ class StubRepository {
Credentials credentials, List<String> ids, EntityAction action) async { Credentials credentials, List<String> ids, EntityAction action) async {
final url = credentials.url + '/stubs/bulk'; final url = credentials.url + '/stubs/bulk';
final dynamic response = await webClient.post(url, credentials.token, final dynamic response = await webClient.post(url, credentials.token,
data: json.encode({'ids': ids, 'action': '$action'})); data: json.encode({'ids': ids, 'action': action.toApiParam()}));
final StubListResponse stubResponse = final StubListResponse stubResponse =
serializers.deserializeWith(StubListResponse.serializer, response); serializers.deserializeWith(StubListResponse.serializer, response);