-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseHelper.cs
More file actions
697 lines (583 loc) · 33.5 KB
/
DatabaseHelper.cs
File metadata and controls
697 lines (583 loc) · 33.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JasperFx.Core;
using Microsoft.Data.SqlClient;
using Shared.Logger;
using TransactionProcessor.Database.Contexts;
using TransactionProcessor.Database.Entities;
namespace EstateReportingAPI.IntegrationTests;
using System.Reflection;
using System.Text;
using Microsoft.EntityFrameworkCore;
public class DatabaseHelper{
private readonly EstateManagementContext Context;
public DatabaseHelper(EstateManagementContext context){
this.Context = context;
}
public async Task DeleteAllMerchants(){
List<Merchant> merchants = await this.Context.Merchants.ToListAsync();
this.Context.RemoveRange(merchants);
await this.Context.SaveChangesAsync();
}
public async Task DeleteAllContracts(){
List<Contract> contracts = await this.Context.Contracts.ToListAsync();
List<ContractProduct> contractProducts = await this.Context.ContractProducts.ToListAsync();
this.Context.RemoveRange(contractProducts);
this.Context.RemoveRange(contracts);
await this.Context.SaveChangesAsync();
}
public async Task AddResponseCode(Int32 code, String description){
await this.Context.Database.OpenConnectionAsync(CancellationToken.None);
try{
StringBuilder builder = new StringBuilder();
builder.AppendLine("SET IDENTITY_INSERT dbo.ResponseCodes ON");
String sql = $"insert into dbo.ResponseCodes(ResponseCode, Description) SELECT {code}, '{description}'";
builder.AppendLine(sql);
builder.AppendLine("SET IDENTITY_INSERT dbo.ResponseCodes OFF");
await this.Context.Database.ExecuteSqlRawAsync(builder.ToString(), CancellationToken.None);
}
finally{
await this.Context.Database.CloseConnectionAsync();
}
}
public async Task AddCalendarYear(Int32 year){
List<DateTime> datesInYear = this.GetDatesForYear(year);
await this.AddCalendarDates(datesInYear);
}
public async Task AddCalendarDates(List<DateTime> dates){
foreach (DateTime dateTime in dates){
Calendar c = dateTime.ToCalendar();
await this.Context.Calendar.AddAsync(c);
}
await this.Context.SaveChangesAsync();
}
public async Task AddCalendarDate(DateTime date){
Calendar c = date.ToCalendar();
await this.Context.Calendar.AddAsync(c);
await this.Context.SaveChangesAsync();
}
public List<DateTime> GetDatesForYear(Int32 year){
List<DateTime> datesInYear = new List<DateTime>();
DateTime startDate = new DateTime(year, 1, 1);
DateTime endDate = new DateTime(year, 12, 31);
for (DateTime currentDate = startDate; currentDate <= endDate; currentDate = currentDate.AddDays(1)){
datesInYear.Add(currentDate);
}
return datesInYear;
}
public async Task<Transaction> AddTransactionOld(DateTime dateTime,
String merchantName,
String contractName,
String contractProductName,
String responseCode,
Decimal? transactionAmount = null,
Int32? transactionReportingId = null,
String authCode=null){
var merchant = await this.Context.Merchants.SingleOrDefaultAsync(m => m.Name == merchantName);
if (merchant == null){
throw new Exception($"No Merchant found with name {merchantName}");
}
var contract = await this.Context.Contracts.SingleOrDefaultAsync(c => c.Description == contractName);
if (contract == null){
throw new Exception($"No Contact record found with description {contractName}");
}
var @operator = await this.Context.Operators.SingleOrDefaultAsync(o => o.OperatorId == contract.OperatorId);
if (@operator == null){
throw new Exception($"No Operator record found with for contract {contractName}");
}
var contractProduct = await this.Context.ContractProducts.SingleOrDefaultAsync(cp => cp.ContractId == contract.ContractId &&
cp.ProductName == contractProductName);
if (contractProduct == null){
throw new Exception($"No Contact Product record found with description {contractProductName} for contract {contractName}");
}
if (contractProduct.Value.HasValue){
transactionAmount = contractProduct.Value.GetValueOrDefault();
}
else{
if (transactionAmount.HasValue == false){
transactionAmount = 75.00m;
}
}
Transaction transaction = new Transaction
{
MerchantId = merchant.MerchantId,
TransactionDate = dateTime.Date,
TransactionDateTime = dateTime,
IsCompleted = true,
AuthorisationCode = "ABCD1234",
DeviceIdentifier = "testdevice1",
//EstateOperatorReportingId = estateOperator.OperatorReportingId,
OperatorId = @operator.OperatorId,
ContractId = contract.ContractId,
ContractProductId = contractProduct.ContractProductId,
IsAuthorised = responseCode == "0000",
ResponseCode = responseCode,
TransactionAmount = transactionAmount.GetValueOrDefault(),
TransactionId = Guid.NewGuid(),
TransactionTime = dateTime.TimeOfDay,
TransactionType = "Sale",
ResponseMessage = responseCode == "0000" ? "SUCCESS" : "FAILED",
TransactionNumber = "0001",
TransactionReference = "Ref1",
TransactionSource = 1
};
if (transactionReportingId.HasValue){
transaction.TransactionReportingId = transactionReportingId.Value;
transaction.TransactionNumber = transactionReportingId.Value.ToString("0000");
}
if (String.IsNullOrEmpty(authCode) == false){
transaction.AuthorisationCode = authCode;
}
await this.Context.Transactions.AddAsync(transaction, CancellationToken.None);
if (transactionReportingId.HasValue){
await this.Context.SaveChangesWithIdentityInsert<Transaction>();
}
else{
await this.Context.SaveChangesAsync(CancellationToken.None);
}
return transaction;
}
public async Task<Transaction> BuildTransactionX(DateTime dateTime,
Guid merchantId,
Guid operatorId,
Guid contractId,
Guid contractProductId,
String responseCode,
Decimal? transactionAmount = null,
Int32? transactionReportingId = null,
String authCode = null)
{
//var contract = await this.Context.Contracts.SingleOrDefaultAsync(c => c.Description == contractName);
//if (contract == null)
//{
// throw new Exception($"No Contact record found with description {contractName}");
//}
//var @operator = await this.Context.Operators.SingleOrDefaultAsync(o => o.OperatorId == contract.OperatorId);
//if (@operator == null)
//{
// throw new Exception($"No Operator record found with for contract {contractName}");
//}
//var contractProduct = await this.Context.ContractProducts.SingleOrDefaultAsync(cp => cp.ContractId == contract.ContractId &&
// cp.ProductName == contractProductName);
//if (contractProduct == null)
//{
// throw new Exception($"No Contact Product record found with description {contractProductName} for contract {contractName}");
//}
//if (contractProduct.Value.HasValue)
//{
// transactionAmount = contractProduct.Value.GetValueOrDefault();
//}
//else
//{
if (transactionAmount.HasValue == false)
{
transactionAmount = 75.00m;
}
//}
Transaction transaction = new Transaction
{
MerchantId = merchantId,
TransactionDate = dateTime.Date,
TransactionDateTime = dateTime,
IsCompleted = true,
AuthorisationCode = "ABCD1234",
DeviceIdentifier = "testdevice1",
OperatorId = operatorId,
ContractId = contractId,
ContractProductId = contractProductId,
IsAuthorised = responseCode == "0000",
ResponseCode = responseCode,
TransactionAmount = transactionAmount.GetValueOrDefault(),
TransactionId = Guid.NewGuid(),
TransactionTime = dateTime.TimeOfDay,
TransactionType = "Sale",
ResponseMessage = responseCode == "0000" ? "SUCCESS" : "FAILED",
TransactionNumber = "0001",
TransactionReference = "Ref1",
TransactionSource = 1
};
if (transactionReportingId.HasValue)
{
transaction.TransactionReportingId = transactionReportingId.Value;
transaction.TransactionNumber = transactionReportingId.Value.ToString("0000");
}
if (String.IsNullOrEmpty(authCode) == false)
{
transaction.AuthorisationCode = authCode;
}
return transaction;
}
public async Task AddTransactionsX(List<Transaction> transactions) {
foreach (Transaction transaction in transactions) {
await this.Context.Transactions.AddAsync(transaction, CancellationToken.None);
}
if (transactions.Any(t => t.TransactionReportingId> 0))
{
await this.Context.SaveChangesWithIdentityInsert<Transaction>();
}
else
{
await this.Context.SaveChangesAsync(CancellationToken.None);
}
}
public async Task<Int32> AddEstate(String estateName, String reference){
Estate estate = new(){
CreatedDateTime = DateTime.Now,
EstateId = Guid.NewGuid(),
Name = estateName,
Reference = reference
};
await this.Context.Estates.AddAsync(estate);
await this.Context.SaveChangesAsync(CancellationToken.None);
return estate.EstateReportingId;
}
public async Task AddOperators(String estateName, List<String> operators)
{
Estate? estate = await this.Context.Estates.SingleOrDefaultAsync(e => e.Name == estateName);
if (estate == null)
{
throw new Exception($"No estate found with name {estateName}");
}
foreach (String operatorName in operators) {
Operator @operator = new Operator {
EstateId = estate.EstateId,
Name = operatorName,
OperatorId = Guid.NewGuid(),
RequireCustomMerchantNumber = false,
RequireCustomTerminalNumber = false
};
await this.Context.Operators.AddAsync(@operator);
}
await this.Context.SaveChangesAsync(CancellationToken.None);
}
public async Task<Int32> AddOperator(String estateName, String operatorName)
{
Estate? estate = await this.Context.Estates.SingleOrDefaultAsync(e => e.Name == estateName);
if (estate == null)
{
throw new Exception($"No estate found with name {estateName}");
}
Operator @operator = new Operator
{
EstateId = estate.EstateId,
Name = operatorName,
OperatorId = Guid.NewGuid(),
RequireCustomMerchantNumber = false,
RequireCustomTerminalNumber = false
};
await this.Context.Operators.AddAsync(@operator);
await this.Context.SaveChangesAsync(CancellationToken.None);
return @operator.OperatorReportingId;
}
public async Task<Int32> AddMerchant(String estateName, String merchantName, DateTime lastSaleDateTime,
List<(String addressLine1, String town, String postCode, String region)> addressList=null){
Estate? estate = await this.Context.Estates.SingleOrDefaultAsync(e => e.Name == estateName);
if (estate == null){
throw new Exception($"No estate found with name {estateName}");
}
Merchant merchant = new Merchant{
EstateId = estate.EstateId,
MerchantId = Guid.NewGuid(),
Name = merchantName,
LastSaleDate = lastSaleDateTime.Date,
LastSaleDateTime = lastSaleDateTime
};
await this.Context.Merchants.AddAsync(merchant);
await this.Context.SaveChangesAsync(CancellationToken.None);
if (addressList != null){
var savedMerchant = await this.Context.Merchants.SingleOrDefaultAsync(m => m.MerchantId == merchant.MerchantId);
if (savedMerchant == null){
throw new Exception($"Error saving merchant {merchant.Name}");
}
foreach ((String addressLine1, String town, String postCode, String region) address in addressList){
await this.Context.MerchantAddresses.AddAsync(new MerchantAddress{
AddressId = Guid.NewGuid(),
AddressLine1 = address.addressLine1,
Region = address.region,
Town = address.town,
PostalCode = address.postCode,
MerchantId = savedMerchant.MerchantId
});
}
}
return merchant.MerchantReportingId;
}
public async Task AddMerchants(String estateName,
List<(String merchantName, DateTime lastSaleDateTime, List<(String addressLine1, String town, String postCode, String region)> addressList)> merchants) {
Estate? estate = await this.Context.Estates.SingleOrDefaultAsync(e => e.Name == estateName);
if (estate == null) {
throw new Exception($"No estate found with name {estateName}");
}
foreach (var merchant1 in merchants) {
Merchant merchant = new Merchant {
EstateId = estate.EstateId,
MerchantId = Guid.NewGuid(),
Name = merchant1.merchantName,
LastSaleDate = merchant1.lastSaleDateTime.Date,
LastSaleDateTime = merchant1.lastSaleDateTime
};
await this.Context.Merchants.AddAsync(merchant);
if (merchant1.addressList != null) {
foreach ((String addressLine1, String town, String postCode, String region) address in merchant1.addressList) {
await this.Context.MerchantAddresses.AddAsync(new MerchantAddress {
AddressId = Guid.NewGuid(),
AddressLine1 = address.addressLine1,
Region = address.region,
Town = address.town,
PostalCode = address.postCode,
MerchantId = merchant.MerchantId
});
}
}
await this.Context.SaveChangesAsync(CancellationToken.None);
}
}
public async Task<Int32> AddContract(String estateName, String contractName, String operatorName){
Estate? estate = await this.Context.Estates.SingleOrDefaultAsync(e => e.Name == estateName);
if (estate == null){
throw new Exception($"No estate found with name {estateName}");
}
// Look up operator
var @operator = await this.Context.Operators.SingleOrDefaultAsync(eo => eo.Name == operatorName);
if (@operator == null){
throw new Exception($"No Operator found with name {operatorName}");
}
Contract contract = new Contract{
EstateId = estate.EstateId,
ContractId = Guid.NewGuid(),
Description = contractName,
OperatorId = @operator.OperatorId,
};
await this.Context.Contracts.AddAsync(contract);
await this.Context.SaveChangesAsync(CancellationToken.None);
return contract.ContractReportingId;
}
public async Task AddContractWithProducts(String estateName, String contractName, String operatorName, List<(String productName, Int32 productType, Decimal? value)> products){
await this.AddContract(estateName, contractName, operatorName);
foreach ((String productName, Int32 productType, Decimal? value) product in products){
await this.AddContractProduct(contractName, product.productName, product.productType, product.value);
}
}
public async Task AddContractProduct(String contractName, String productName, Int32 productType, Decimal? value){
Contract? contract = await this.Context.Contracts.SingleOrDefaultAsync(c => c.Description == contractName);
if (contract == null){
throw new Exception($"No Contact record found with description {contractName}");
}
ContractProduct contractProduct = new ContractProduct{
ContractId = contract.ContractId,
DisplayText = productName,
ContractProductId = Guid.NewGuid(),
ProductName = productName,
ProductType = productType,
Value = value
};
await this.Context.ContractProducts.AddAsync(contractProduct);
await this.Context.SaveChangesAsync(CancellationToken.None);
ContractProductTransactionFee fee = new ContractProductTransactionFee
{
CalculationType = 0,
ContractProductId = contractProduct.ContractProductId,
Description = "Merchant Commission",
FeeType = 0,
IsEnabled = true,
ContractProductTransactionFeeId = Guid.NewGuid(),
Value = 0.5m
};
await this.Context.ContractProductTransactionFees.AddAsync(fee);
await this.Context.SaveChangesAsync(CancellationToken.None);
}
public async Task AddMerchantSettlementFee(Guid settlementId, DateTime feeCalculatedDateTime, Guid merchantId, Guid operatorId, Guid contractId, Guid contractProductId, CancellationToken cancellationToken, Boolean isSettled = false){
var productTransactionFee = await this.Context.ContractProductTransactionFees.SingleOrDefaultAsync(f => f.ContractProductId == contractProductId, cancellationToken);
Transaction transaction = new(){
ContractProductId = contractProductId,
MerchantId = merchantId,
OperatorId = operatorId,
ContractId = contractId,
IsAuthorised = true,
IsCompleted = true,
TransactionAmount = 1.00m,
TransactionDate = feeCalculatedDateTime.Date,
TransactionDateTime = feeCalculatedDateTime,
TransactionSource = 1,
TransactionTime = feeCalculatedDateTime.TimeOfDay,
TransactionId = Guid.NewGuid(),
};
await this.Context.Transactions.AddAsync(transaction,cancellationToken);
await this.Context.SaveChangesAsync(cancellationToken);
MerchantSettlementFee fee = new(){
FeeCalculatedDateTime = feeCalculatedDateTime,
MerchantId = merchantId,
CalculatedValue = productTransactionFee.Value,
FeeValue = productTransactionFee.Value,
IsSettled = isSettled,
SettlementId = settlementId,
ContractProductTransactionFeeId = productTransactionFee.ContractProductTransactionFeeId,
TransactionId = transaction.TransactionId
};
await this.Context.MerchantSettlementFees.AddAsync(fee, cancellationToken);
await this.Context.SaveChangesAsync(cancellationToken);
}
public async Task<(Decimal settledTransactions, Decimal pendingSettlementTransactions, Decimal settlementFees, Decimal pendingSettlementFees)> AddSettlementRecord(Guid estateId, Guid merchantId, Guid operatorId, DateTime settlementDate, Int32 settledTransactionCount, Int32 pendingSettlementTransactionCount){
List<Transaction> settledTransactions = new();
List<Transaction> pendingSettlementTransactions = new();
List<(Decimal feeValue, Decimal calulatedValue, Guid contractProductTransactionFeeId, Boolean isSettled, Guid transactionId)> settlementFees = new();
List<(Decimal feeValue, Decimal calulatedValue, Guid contractProductTransactionFeeId, Boolean isSettled, Guid transactionId)> pendingSettlementFees = new();
var feeId = Guid.NewGuid();
//var @operator = await this.Context.Operators.SingleOrDefaultAsync(o => o.Name == operatorName);
var contractProducts = await this.Context.ContractProducts.Join(
this.Context.Contracts,
c => c.ContractId,
o => o.ContractId,
(cp, c) => new{
cp,
c
}
)
.Join(this.Context.ContractProductTransactionFees,
c => c.cp.ContractProductId,
f => f.ContractProductId,
(c, f) => new
{
c.cp,
c.c,
f
}).Where(x => x.c.OperatorId == operatorId).FirstAsync();
for (int i = 1; i <= settledTransactionCount; i++){
Transaction transaction = await this.BuildTransactionX(settlementDate.AddDays(-1), merchantId, operatorId, contractProducts.c.ContractId,
contractProducts.cp.ContractProductId,"0000", null);
settledTransactions.Add(transaction);
settlementFees.Add((0.5m, 0.5m * i, contractProducts.f.ContractProductTransactionFeeId, true, transaction.TransactionId));
}
for (int i = 1; i <= pendingSettlementTransactionCount; i++){
Transaction transaction = await this.BuildTransactionX(settlementDate.AddDays(-1), merchantId, operatorId, contractProducts.c.ContractId,
contractProducts.cp.ContractProductId, "0000", null);
pendingSettlementTransactions.Add(transaction);
pendingSettlementFees.Add((0.5m, 0.5m * i, contractProducts.f.ContractProductTransactionFeeId, false, transaction.TransactionId));
}
await this.AddTransactionsX(settledTransactions);
await this.AddTransactionsX(pendingSettlementTransactions);
await this.AddSettlementRecordWithFees(settlementDate, estateId, merchantId, pendingSettlementFees, settlementFees);
return (settledTransactions.Sum(s => s.TransactionAmount), pendingSettlementTransactions.Sum(p => p.TransactionAmount),
settlementFees.Sum(s => s.calulatedValue), pendingSettlementFees.Sum(p => p.calulatedValue));
}
public async Task AddSettlementRecordWithFees(DateTime dateTime,
Guid estateId,
Guid merchantId,
List<(Decimal feeValue, Decimal calulatedValue, Guid contractProductTransactionFeeId, Boolean isSettled, Guid transactionId)> pendingFeesList,
List<(Decimal feeValue, Decimal calulatedValue, Guid contractProductTransactionFeeId, Boolean isSettled, Guid transactionId)> settledFeesList){
Settlement settlementRecord = new Settlement{
ProcessingStarted = true,
IsCompleted = pendingFeesList.Count == 0,
EstateId = estateId,
MerchantId = merchantId,
ProcessingStartedDateTIme = dateTime,
SettlementDate = dateTime,
SettlementId = Guid.NewGuid(),
};
await this.Context.Settlements.AddAsync(settlementRecord, CancellationToken.None);
await this.Context.SaveChangesAsync(CancellationToken.None);
foreach ((Decimal feeValue, Decimal calulatedValue, Guid contractProductTransactionFeeId, Boolean isSettled, Guid transactionId) fee in pendingFeesList){
MerchantSettlementFee merchantSettlementFee = new MerchantSettlementFee{
MerchantId = merchantId,
SettlementId = settlementRecord.SettlementId,
IsSettled = fee.isSettled,
FeeCalculatedDateTime = dateTime,
TransactionId = fee.transactionId,
ContractProductTransactionFeeId = fee.contractProductTransactionFeeId,
CalculatedValue = fee.calulatedValue,
FeeValue = fee.feeValue
};
await this.Context.MerchantSettlementFees.AddAsync(merchantSettlementFee, CancellationToken.None);
}
foreach ((Decimal feeValue, Decimal calulatedValue, Guid contractProductTransactionFeeId, Boolean isSettled, Guid transactionId) fee in settledFeesList)
{
MerchantSettlementFee merchantSettlementFee = new MerchantSettlementFee
{
MerchantId = merchantId,
SettlementId = settlementRecord.SettlementId,
IsSettled = fee.isSettled,
FeeCalculatedDateTime = dateTime,
TransactionId = fee.transactionId,
ContractProductTransactionFeeId = fee.contractProductTransactionFeeId,
CalculatedValue = fee.calulatedValue,
FeeValue = fee.feeValue
};
await this.Context.MerchantSettlementFees.AddAsync(merchantSettlementFee, CancellationToken.None);
}
await this.Context.SaveChangesAsync(CancellationToken.None);
}
public async Task RunTodaysTransactionsSummaryProcessing(DateTime date)
{
await this.Context.Database.ExecuteSqlAsync(
$"exec spBuildTodaysTransactions @date={date.Date.ToString("yyyy-MM-dd")}", CancellationToken.None);
}
public async Task RunHistoricTransactionsSummaryProcessing(DateTime date)
{
await this.Context.Database.ExecuteSqlAsync(
$"exec spBuildHistoricTransactions @date={date.Date.ToString("yyyy-MM-dd")}", CancellationToken.None);
}
public async Task RunSettlementSummaryProcessing(DateTime date)
{
await this.Context.Database.ExecuteSqlAsync(
$"exec spBuildSettlementSummary @date={date.Date.ToString("yyyy-MM-dd")}", CancellationToken.None);
}
public async Task CreateStoredProcedures(CancellationToken cancellationToken)
{
String executingAssemblyLocation = Assembly.GetExecutingAssembly().Location;
String executingAssemblyFolder = Path.GetDirectoryName(executingAssemblyLocation);
String scriptsFolder = $@"{executingAssemblyFolder}/StoredProcedures";
String[] directiories = Directory.GetDirectories(scriptsFolder);
if (directiories.Length == 0)
{
var list = new List<string> { scriptsFolder };
directiories = list.ToArray();
}
directiories = directiories.OrderBy(d => d).ToArray();
foreach (String directiory in directiories)
{
String[] sqlFiles = Directory.GetFiles(directiory, "*.sql");
foreach (String sqlFile in sqlFiles.OrderBy(x => x))
{
Logger.LogDebug($"About to create Stored Procedure [{sqlFile}]");
String sql = System.IO.File.ReadAllText(sqlFile);
// Check here is we need to replace a Database Name
if (sql.Contains("{DatabaseName}"))
{
sql = sql.Replace("{DatabaseName}", this.Context.Database.GetDbConnection().Database);
}
// Create the new view using the original sql from file
await this.Context.Database.ExecuteSqlRawAsync(sql, cancellationToken);
Logger.LogDebug($"Created Stored Procedure [{sqlFile}] successfully.");
}
}
}
public async Task<Guid?> GetOperatorId(Int32 operatorReportingId, CancellationToken cancellationToken) {
return await this.Context.Operators.Where(o => o.OperatorReportingId == operatorReportingId).Select(o => o.OperatorId).SingleOrDefaultAsync(cancellationToken);
}
public async Task<Guid?> GetMerchantId(Int32 merchantReportingId, CancellationToken cancellationToken)
{
return await this.Context.Merchants.Where(o => o.MerchantReportingId == merchantReportingId).Select(o => o.MerchantId).SingleOrDefaultAsync(cancellationToken);
}
}
public static class IdentityHelpers{
public static Task EnableIdentityInsert<T>(this DbContext context) => SetIdentityInsert<T>(context, enable:true);
public static Task DisableIdentityInsert<T>(this DbContext context) => SetIdentityInsert<T>(context, enable:false);
private static Task SetIdentityInsert<T>(DbContext context, bool enable){
var entityType = context.Model.FindEntityType(typeof(T));
var value = enable ? "ON" : "OFF";
String schema = entityType.GetSchema();
return context.Database.ExecuteSqlRawAsync($"SET IDENTITY_INSERT {schema}.[{entityType.GetTableName()}] {value}");
}
public static async Task SaveChangesWithIdentityInsert<T>(this DbContext context){
using var transaction = context.Database.BeginTransaction();
await context.EnableIdentityInsert<T>();
await context.SaveChangesAsync();
await context.DisableIdentityInsert<T>();
await transaction.CommitAsync();
}
}