Skip to content

Commit

Permalink
chore: added backup queue for purchases
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoreMihai committed Mar 22, 2022
1 parent 5020c66 commit 8bca6d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Common/Messages/BackupMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Common.Messages
{
public class BackupMessage
{
public int PlanId { get; set; }

public string SaleType { get; set; }

public int Price { get; set; }
}
}
4 changes: 4 additions & 0 deletions WebHook/Model/Hook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class Hook
public List<Repository> repositories_removed { get; set; }
public Sender sender { get; set; }
public MarketplacePurchase marketplace_purchase { get; set; }

public MarketplacePurchase previous_marketplace_purchase { get; set; }

public int number { get; set; }
public PullRequest pull_request { get; set; }
Expand Down Expand Up @@ -336,11 +338,13 @@ public class MarketplacePurchase
public Account account { get; set; }

public Plan plan { get; set; }
public bool? on_free_trial { get; set; }
}

public class Plan
{
public int id { get; set; }
public int monthly_price_in_cents { get; set; }
}
}
}
29 changes: 27 additions & 2 deletions WebHook/WebHookFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static Task<IActionResult> Hook(
var installationTable = storageAccount.CreateCloudTableClient().GetTableReference("installation");
var marketplaceTable = storageAccount.CreateCloudTableClient().GetTableReference("marketplace");
var settingsTable = storageAccount.CreateCloudTableClient().GetTableReference("settings");
var backupQueue = storageAccount.CreateCloudQueueClient().GetQueueReference("backup");

return Run(
req,
Expand All @@ -40,6 +41,7 @@ public static Task<IActionResult> Hook(
installationTable,
marketplaceTable,
settingsTable,
backupQueue,
logger);
}

Expand All @@ -51,6 +53,7 @@ public static async Task<IActionResult> Run(
CloudTable installationTable,
CloudTable marketplaceTable,
CloudTable settingsTable,
CloudQueue backupMessages,
ILogger logger)
{
var hookEvent = req.Headers.GetValues("X-GitHub-Event").First();
Expand All @@ -67,7 +70,7 @@ public static async Task<IActionResult> Run(
.ConfigureAwait(false);
break;
case "marketplace_purchase":
result = await ProcessMarketplacePurchaseAsync(hook, marketplaceTable, logger).ConfigureAwait(false);
result = await ProcessMarketplacePurchaseAsync(hook, marketplaceTable, backupMessages, logger).ConfigureAwait(false);
break;
}

Expand Down Expand Up @@ -314,7 +317,7 @@ await routerMessages.AddMessageAsync(new CloudQueueMessage(JsonConvert.Serialize
return "truth";
}

private static async Task<string> ProcessMarketplacePurchaseAsync(Hook hook, CloudTable marketplaceTable, ILogger logger)
private static async Task<string> ProcessMarketplacePurchaseAsync(Hook hook, CloudTable marketplaceTable, CloudQueue backupMessages, ILogger logger)
{
switch (hook.action)
{
Expand All @@ -327,6 +330,28 @@ private static async Task<string> ProcessMarketplacePurchaseAsync(Hook hook, Clo
allowedPrivate = KnownGitHubs.Plans[hook.marketplace_purchase.plan.id];
}

if (hook.marketplace_purchase.on_free_trial != true && KnownGitHubs.Plans.ContainsKey(hook.marketplace_purchase.plan.id) && KnownGitHubs.Plans[hook.marketplace_purchase.plan.id] != 0)
{
// TODO: check if call to table can be reliable removed, use previous purchase instead
var mktplc = await marketplaceTable.ExecuteAsync(
TableOperation.Retrieve<Common.TableModels.Marketplace>(hook.marketplace_purchase.account.id.ToString(), hook.marketplace_purchase.account.login));

var recurrentSale = "new";

if (mktplc?.Result != null)
{
recurrentSale = "recurrent";
}

await backupMessages.AddMessageAsync(new CloudQueueMessage(JsonConvert.SerializeObject(
new BackupMessage
{
PlanId = hook.marketplace_purchase.plan.id,
Price = hook.marketplace_purchase.plan.monthly_price_in_cents / 100,
SaleType = recurrentSale
})));
}

await marketplaceTable.ExecuteAsync(TableOperation.InsertOrMerge(new Marketplace(hook.marketplace_purchase.account.id, hook.marketplace_purchase.account.login)
{
AccountType = hook.marketplace_purchase.account.type,
Expand Down

0 comments on commit 8bca6d0

Please sign in to comment.