Skip to content

Commit

Permalink
Merge pull request #16 from IliyanIlievPH/12
Browse files Browse the repository at this point in the history
Closes #12
  • Loading branch information
starkmsu committed Jun 5, 2020
2 parents d78af2a + a63942b commit 0274067
Show file tree
Hide file tree
Showing 20 changed files with 256 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace MAVN.Service.DashboardStatistics.Client.Models.Customers
public class CustomersListRequestModel : BasePeriodRequestModel
{
/// <summary>
/// Id of the partner, used for filtering
/// Ids of partners, used for filtering
/// </summary>
public Guid? PartnerId { get; set; }
public Guid[] PartnerIds { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using JetBrains.Annotations;

namespace MAVN.Service.DashboardStatistics.Client.Models.Customers
Expand Down Expand Up @@ -29,6 +29,11 @@ public class CustomersStatisticResponse
/// </summary>
public int TotalNewCustomers { get; set; }

/// <summary>
/// The count of repeat active customers in a specific period.
/// </summary>
public int TotalRepeatCustomers { get; set; }

/// <summary>
/// Represents a list of new registered customers by days for selected period
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MAVN.Service.DashboardStatistics.Domain.Enums
{
public enum ActivityType
{
VoucherBought,
VoucherUsed,
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;

namespace MAVN.Service.DashboardStatistics.Domain.Models.Customers
{
Expand Down Expand Up @@ -27,6 +27,11 @@ public class CustomersStatistic
/// </summary>
public int TotalNewCustomers { get; set; }

/// <summary>
/// The count of repeat active customers in a specific period.
/// </summary>
public int TotalRepeatCustomers { get; set; }

/// <summary>
/// The list of registered customers per days in a specific period.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using System;
using System.Threading.Tasks;

namespace MAVN.Service.DashboardStatistics.Domain.Repositories
{
public interface ICustomerActivityRepository
{
Task<int> GetCountAsync(DateTime startDate, DateTime endDate);
Task<int> GetCountAsync(DateTime startDate, DateTime endDate, Guid[] partnerIds);

Task InsertAsync(Guid customerId, DateTime activityDate);

Task<int> GetRepeatCountAsync(DateTime startDate, DateTime endDate, Guid[] partnerIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace MAVN.Service.DashboardStatistics.Domain.Repositories
{
public interface ICustomerRegistrationRepository
{
Task<int> GetCountSync(DateTime endDate, Guid? partnerId);
Task<int> GetCountSync(DateTime endDate, Guid[] partnerIds);

Task<IReadOnlyDictionary<DateTime, int>> GetCountPerDayAsync(DateTime startDate, DateTime endDate);
Task<IReadOnlyDictionary<DateTime, int>> GetCountPerDayAsync(DateTime startDate, DateTime endDate, Guid[] partnerIds);

Task InsertIfNotExistsAsync(Guid customerId, Guid? partnerId, DateTime registrationDate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace MAVN.Service.DashboardStatistics.Domain.Services
{
public interface ICustomerStatisticService
{
Task<CustomersStatistic> GetAsync(DateTime fromDate, DateTime toDate, Guid? partnerId);
Task<CustomersStatistic> GetAsync(DateTime fromDate, DateTime toDate, Guid[] partnerIds);

Task AddRegistrationDateAsync(Guid customerId, Guid? partnerId, DateTime registrationDate,
VoucherOperationType operationType, decimal amount, string currency);

Task AddActivityDateAsync(Guid customerId, DateTime activityDate);
Task AddActivityDateAsync(Guid customerId, DateTime activityDate, Guid? partnerId, ActivityType? activityType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public class CustomerStatisticService : ICustomerStatisticService
_voucherOperationsStatisticService = voucherOperationsStatisticService;
}

public async Task<CustomersStatistic> GetAsync(DateTime startDate, DateTime endDate, Guid? partnerId)
public async Task<CustomersStatistic> GetAsync(DateTime startDate, DateTime endDate, Guid[] partnerIds)
{
var newCustomersPerDate = new List<CustomersCountAtDate>();

var totalCustomers = await _customerRegistrationRepository.GetCountSync(endDate, partnerId);
var totalCustomers = await _customerRegistrationRepository.GetCountSync(endDate, partnerIds);

if (totalCustomers == 0)
{
Expand All @@ -41,13 +41,15 @@ public async Task<CustomersStatistic> GetAsync(DateTime startDate, DateTime endD
TotalCustomers = 0,
TotalNewCustomers = 0,
TotalNonActiveCustomers = 0,
TotalRepeatCustomers = 0,
};
}

var customersCountPerDayTask = _customerRegistrationRepository.GetCountPerDayAsync(startDate, endDate);
var activeCustomersCountTask = _customerActivityRepository.GetCountAsync(startDate, endDate);
var customersCountPerDayTask = _customerRegistrationRepository.GetCountPerDayAsync(startDate, endDate, partnerIds);
var activeCustomersCountTask = _customerActivityRepository.GetCountAsync(startDate, endDate, partnerIds);
var repeaterCustomersCountTask = _customerActivityRepository.GetRepeatCountAsync(startDate, endDate, partnerIds);

await Task.WhenAll(customersCountPerDayTask, activeCustomersCountTask);
await Task.WhenAll(customersCountPerDayTask, activeCustomersCountTask, repeaterCustomersCountTask);

for (var date = startDate.Date; date <= endDate.Date; date = date.AddDays(1))
{
Expand All @@ -62,7 +64,8 @@ public async Task<CustomersStatistic> GetAsync(DateTime startDate, DateTime endD
TotalActiveCustomers = activeCustomersCountTask.Result,
TotalNonActiveCustomers = totalCustomers - activeCustomersCountTask.Result,
TotalNewCustomers = customersCountPerDayTask.Result.Sum(o => o.Value),
NewCustomers = newCustomersPerDate
NewCustomers = newCustomersPerDate,
TotalRepeatCustomers = repeaterCustomersCountTask.Result,
};

return statistic;
Expand All @@ -84,7 +87,7 @@ public async Task<CustomersStatistic> GetAsync(DateTime startDate, DateTime endD
await _voucherOperationsStatisticService.UpdateVoucherOperationsStatistic(model);
}

public Task AddActivityDateAsync(Guid customerId, DateTime activityDate)
public Task AddActivityDateAsync(Guid customerId, DateTime activityDate, Guid? partnerId, ActivityType? activityType)
{
return _customerActivityRepository.InsertAsync(customerId, activityDate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override async Task ProcessMessageAsync(BonusReceivedEvent message)

try
{
await _customerStatisticService.AddActivityDateAsync(customerId, message.Timestamp);
await _customerStatisticService.AddActivityDateAsync(customerId, message.Timestamp, null, null);
}
catch (Exception exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override async Task ProcessMessageAsync(PartnersPaymentTokensReservedE

try
{
await _customerStatisticService.AddActivityDateAsync(customerId, message.Timestamp);
await _customerStatisticService.AddActivityDateAsync(customerId, message.Timestamp, null, null);
}
catch (Exception exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ protected override async Task ProcessMessageAsync(SmartVoucherSoldEvent message)
{
await _customerStatisticService.AddRegistrationDateAsync(message.CustomerId, message.PartnerId,
message.Timestamp, VoucherOperationType.Buy, message.Amount, message.Currency);

await _customerStatisticService.AddActivityDateAsync(message.CustomerId, message.Timestamp,
message.PartnerId, ActivityType.VoucherBought);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

namespace MAVN.Service.DashboardStatistics.DomainServices.RabbitMq.Subscribers
{
public class SmartVoucherUsedHandler : JsonRabbitSubscriber<SmartVoucherUsedEvent>
public class SmartVoucherUsedSubscriber : JsonRabbitSubscriber<SmartVoucherUsedEvent>
{
private readonly ICustomerStatisticService _customerStatisticService;

public SmartVoucherUsedHandler(
public SmartVoucherUsedSubscriber(
ICustomerStatisticService customerStatisticService,
string connectionString,
string exchangeName,
Expand All @@ -26,6 +26,9 @@ protected override async Task ProcessMessageAsync(SmartVoucherUsedEvent message)
{
await _customerStatisticService.AddRegistrationDateAsync(message.CustomerId, message.PartnerId,
message.Timestamp, VoucherOperationType.Redeem, message.Amount, message.Currency);

await _customerStatisticService.AddActivityDateAsync(message.CustomerId, message.Timestamp,
message.PartnerId, ActivityType.VoucherUsed);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using MAVN.Service.DashboardStatistics.Domain.Enums;

namespace MAVN.Service.DashboardStatistics.MsSqlRepositories.Entities
{
Expand All @@ -17,5 +18,11 @@ public class CustomerActivityEntity

[Column("activity_date")]
public DateTime ActivityDate { get; set; }

[Column("partner_id")]
public Guid? PartnerId { get; set; }

[Column("activity_type")]
public ActivityType? ActivityType { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace MAVN.Service.DashboardStatistics.MsSqlRepositories.Migrations
{
public partial class ExtendActivityEntityWithPartnerIdAndType : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "activity_type",
schema: "dashboard_statistic",
table: "customer_activities",
nullable: true);

migrationBuilder.AddColumn<Guid>(
name: "partner_id",
schema: "dashboard_statistic",
table: "customer_activities",
nullable: true);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "activity_type",
schema: "dashboard_statistic",
table: "customer_activities");

migrationBuilder.DropColumn(
name: "partner_id",
schema: "dashboard_statistic",
table: "customer_activities");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<DateTime>("ActivityDate")
.HasColumnName("activity_date");
b.Property<int?>("ActivityType")
.HasColumnName("activity_type");
b.Property<Guid>("CustomerId")
.HasColumnName("customer_id");
b.Property<Guid?>("PartnerId")
.HasColumnName("partner_id");
b.HasKey("Id");
b.ToTable("customer_activities");
Expand Down
Loading

0 comments on commit 0274067

Please sign in to comment.