-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
1,436 additions
and
741 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
Source/CompanyCommunicator.Common/Services/Recipients/IRecipientsService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// <copyright file="IRecipientsService.cs" company="Microsoft"> | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
// </copyright> | ||
|
||
namespace Microsoft.Teams.Apps.CompanyCommunicator.Common.Services.Recipients | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Teams.Apps.CompanyCommunicator.Common.Repositories.SentNotificationData; | ||
|
||
/// <summary> | ||
/// Recipient service. | ||
/// </summary> | ||
public interface IRecipientsService | ||
{ | ||
/// <summary> | ||
/// Batch the list of recipients. | ||
/// </summary> | ||
/// <param name="recipients">list of recipients.</param> | ||
/// <returns>recipients information.</returns> | ||
Task<RecipientsInfo> BatchRecipients(IEnumerable<SentNotificationDataEntity> recipients); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
Source/CompanyCommunicator.Common/Services/Recipients/RecipientsInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// <copyright file="RecipientsInfo.cs" company="Microsoft"> | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
// </copyright> | ||
|
||
namespace Microsoft.Teams.Apps.CompanyCommunicator.Common.Services.Recipients | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
/// <summary> | ||
/// Recipient information. | ||
/// </summary> | ||
public class RecipientsInfo | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RecipientsInfo"/> class. | ||
/// </summary> | ||
/// <param name="notificationId">notification id.</param> | ||
public RecipientsInfo(string notificationId) | ||
{ | ||
if (string.IsNullOrEmpty(notificationId)) | ||
{ | ||
throw new ArgumentNullException(nameof(notificationId)); | ||
} | ||
|
||
// Initialize properties. | ||
this.TotalRecipientCount = 0; | ||
this.BatchKeys = new List<string>(); | ||
this.HasRecipientsPendingInstallation = false; | ||
this.NotificationId = notificationId; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the notification id. | ||
/// </summary> | ||
public string NotificationId { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the total recipient count of the message. | ||
/// </summary> | ||
public int TotalRecipientCount { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a value indicating whether there are user app installations pending(recipients who have no conversation id in database) for recipients. | ||
/// </summary> | ||
public bool HasRecipientsPendingInstallation { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the batch keys of the recipients. | ||
/// </summary> | ||
public List<string> BatchKeys { get; set; } | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
Source/CompanyCommunicator.Common/Services/Recipients/RecipientsService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// <copyright file="RecipientsService.cs" company="Microsoft"> | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
// </copyright> | ||
|
||
namespace Microsoft.Teams.Apps.CompanyCommunicator.Common.Services.Recipients | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Teams.Apps.CompanyCommunicator.Common.Extensions; | ||
using Microsoft.Teams.Apps.CompanyCommunicator.Common.Repositories.SentNotificationData; | ||
using Microsoft.Teams.Apps.CompanyCommunicator.Common.Utilities; | ||
|
||
/// <summary> | ||
/// Recipients service. | ||
/// </summary> | ||
public class RecipientsService : IRecipientsService | ||
{ | ||
private readonly ISentNotificationDataRepository sentNotificationDataRepository; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RecipientsService"/> class. | ||
/// </summary> | ||
/// <param name="sentNotificationDataRepository">sent notification data repository.</param> | ||
public RecipientsService(ISentNotificationDataRepository sentNotificationDataRepository) | ||
{ | ||
this.sentNotificationDataRepository = sentNotificationDataRepository ?? throw new ArgumentNullException(nameof(sentNotificationDataRepository)); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task<RecipientsInfo> BatchRecipients(IEnumerable<SentNotificationDataEntity> recipients) | ||
{ | ||
if (recipients == null) | ||
{ | ||
throw new ArgumentNullException(nameof(IEnumerable<SentNotificationDataEntity>)); | ||
} | ||
|
||
var notificationId = recipients.FirstOrDefault().PartitionKey; | ||
|
||
var recipientBatches = recipients.AsBatches(Constants.MaximumNumberOfRecipientsInBatch); | ||
var recipientInfo = new RecipientsInfo(notificationId) | ||
{ | ||
TotalRecipientCount = recipients.ToList().Count, | ||
}; | ||
int batchIndex = 1; | ||
foreach (var recipientBatch in recipientBatches) | ||
{ | ||
var recipientBatchList = recipientBatch.ToList(); | ||
|
||
// Update PartitionKey to Batch Key | ||
recipientBatchList.ForEach(s => | ||
{ | ||
s.PartitionKey = PartitionKeyUtility.CreateBatchPartitionKey(s.PartitionKey, batchIndex); | ||
|
||
// Update if there is any recipient which has no conversation id. | ||
recipientInfo.HasRecipientsPendingInstallation |= string.IsNullOrEmpty(s.ConversationId); | ||
}); | ||
|
||
// Store. | ||
await this.sentNotificationDataRepository.BatchInsertOrMergeAsync(recipientBatch); | ||
recipientInfo.BatchKeys.Add(recipientBatch.FirstOrDefault().PartitionKey); | ||
batchIndex++; | ||
} | ||
|
||
return recipientInfo; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
Source/CompanyCommunicator.Common/Utilities/PartitionKeyUtility.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// <copyright file="PartitionKeyUtility.cs" company="Microsoft"> | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
// </copyright> | ||
|
||
namespace Microsoft.Teams.Apps.CompanyCommunicator.Common.Utilities | ||
{ | ||
using System; | ||
|
||
/// <summary> | ||
/// Partition Key utility. | ||
/// </summary> | ||
public static class PartitionKeyUtility | ||
{ | ||
/// <summary> | ||
/// Create the partition key from notification id. | ||
/// </summary> | ||
/// <param name="notificationId">notification id.</param> | ||
/// <param name="batchIndex">batch index.</param> | ||
/// <returns>partition key.</returns> | ||
public static string CreateBatchPartitionKey(string notificationId, int batchIndex) | ||
{ | ||
return $"{notificationId}:{batchIndex}"; | ||
} | ||
|
||
/// <summary> | ||
/// Get the notification id from partition key. | ||
/// </summary> | ||
/// <param name="partitionKey">partition key.</param> | ||
/// <returns>notification id.</returns> | ||
public static string GetNotificationIdFromBatchPartitionKey(string partitionKey) | ||
{ | ||
var result = partitionKey.Split(":"); | ||
if (result.Length != 2) | ||
{ | ||
throw new FormatException("Invalid format of batch partition key"); | ||
} | ||
|
||
return result[0]; | ||
} | ||
|
||
/// <summary> | ||
/// Get the notification id from partition key. | ||
/// </summary> | ||
/// <param name="partitionKey">partition key.</param> | ||
/// <returns>notification id.</returns> | ||
public static string GetBatchIdFromBatchPartitionKey(string partitionKey) | ||
{ | ||
var result = partitionKey.Split(":"); | ||
if (result.Length != 2) | ||
{ | ||
throw new FormatException("Invalid format of batch partition key"); | ||
} | ||
|
||
return result[1]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.