1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00
Teknik/BillingService/Program.cs

136 lines
5.3 KiB
C#
Raw Normal View History

2021-08-12 03:44:15 +02:00
using System;
using System.Collections.Generic;
using System.IO;
2021-11-14 05:28:24 +01:00
using System.Linq;
2021-08-12 03:44:15 +02:00
using System.Reflection;
using CommandLine;
using Microsoft.EntityFrameworkCore;
using Teknik.Areas.Billing;
2021-11-14 05:28:24 +01:00
using Teknik.Areas.Users.Models;
using Teknik.Areas.Users.Utility;
using Teknik.BillingCore;
using Teknik.BillingCore.Models;
2021-08-12 03:44:15 +02:00
using Teknik.Configuration;
using Teknik.Data;
using Teknik.Utilities;
namespace Teknik.BillingService
{
public class Program
{
private static string currentPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
private static string errorFile = Path.Combine(currentPath, "errorLogs.txt");
private static string configPath = currentPath;
private static readonly object dbLock = new object();
private static readonly object scanStatsLock = new object();
public static event Action<string> OutputEvent;
public static int Main(string[] args)
{
try
{
Parser.Default.ParseArguments<ArgumentOptions>(args).WithParsed(options =>
{
if (!string.IsNullOrEmpty(options.Config))
configPath = options.Config;
if (Directory.Exists(configPath))
{
Config config = Config.Load(configPath);
Output(string.Format("[{0}] Started Billing Service Process.", DateTime.Now));
var optionsBuilder = new DbContextOptionsBuilder<TeknikEntities>();
optionsBuilder.UseSqlServer(config.DbConnection);
using (TeknikEntities db = new TeknikEntities(optionsBuilder.Options))
{
if (options.SyncSubscriptions)
{
// Sync subscription information
2021-11-14 05:28:24 +01:00
SyncSubscriptions(config, db);
2021-08-12 03:44:15 +02:00
}
}
Output(string.Format("[{0}] Finished Billing Service Process.", DateTime.Now));
}
else
{
string msg = string.Format("[{0}] Config File does not exist.", DateTime.Now);
File.AppendAllLines(errorFile, new List<string> { msg });
Output(msg);
}
});
}
catch (Exception ex)
{
string msg = string.Format("[{0}] Exception: {1}", DateTime.Now, ex.GetFullMessage(true));
File.AppendAllLines(errorFile, new List<string> { msg });
Output(msg);
}
return -1;
}
2021-11-14 05:28:24 +01:00
public static void SyncSubscriptions(Config config, TeknikEntities db)
{
// Get Biling Service
var billingService = BillingFactory.GetBillingService(config.BillingConfig);
// Get all customers
var customers = billingService.GetCustomers();
if (customers != null)
{
2021-11-21 04:53:28 +01:00
var customerIds = customers.Select(c => c.CustomerId).ToList();
// Find customers that aren't linked anymore
2021-11-21 04:53:28 +01:00
var unlinkedCustomers = db.Users.Select(u => u.BillingCustomer).Where(b => b != null && !customerIds.Contains(b.CustomerId));
foreach (var customer in unlinkedCustomers)
{
BillingHelper.RemoveCustomer(db, customer.CustomerId);
}
}
2021-11-21 04:53:28 +01:00
foreach (var user in db.Users.Include(u => u.BillingCustomer))
2021-11-14 05:28:24 +01:00
{
// Only set/reset their limits if they have a subscription or have subscribed at some point
2021-11-14 05:28:24 +01:00
if (user.BillingCustomer != null)
{
// get the subscriptions for this user
var subscriptions = billingService.GetSubscriptionList(user.BillingCustomer.CustomerId);
var uploadPrice = subscriptions.SelectMany(s => s.Prices).FirstOrDefault(p => p.ProductId == config.BillingConfig.UploadProductId);
2021-11-21 04:53:28 +01:00
// Process upload subscription sync
2021-11-14 05:28:24 +01:00
if (uploadPrice != null)
{
BillingHelper.SetUploadLimits(db, user, uploadPrice.Storage, uploadPrice.FileSize);
}
else
{
BillingHelper.SetUploadLimits(db, user, config.UploadConfig.MaxStorage, config.UploadConfig.MaxUploadFileSize);
2021-11-14 05:28:24 +01:00
}
2021-11-21 04:53:28 +01:00
// Process email subscription sync
2021-11-14 05:28:24 +01:00
var emailPrice = subscriptions.SelectMany(s => s.Prices).FirstOrDefault(p => p.ProductId == config.BillingConfig.EmailProductId);
if (emailPrice != null)
{
BillingHelper.SetEmailLimits(config, user, emailPrice.Storage, true);
}
else
{
BillingHelper.SetEmailLimits(config, user, config.EmailConfig.MaxSize, false);
2021-11-14 05:28:24 +01:00
}
}
}
}
2021-08-12 03:44:15 +02:00
public static void Output(string message)
{
Console.WriteLine(message);
if (OutputEvent != null)
{
OutputEvent(message);
}
}
}
}