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;
|
2021-12-01 05:33:34 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2021-08-12 03:44:15 +02:00
|
|
|
|
using CommandLine;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-11-20 01:59:31 +01:00
|
|
|
|
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
|
2022-05-23 05:51:09 +02:00
|
|
|
|
SyncSubscriptions(config, db).Wait();
|
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-12-01 05:33:34 +01:00
|
|
|
|
public static async Task SyncSubscriptions(Config config, TeknikEntities db)
|
2021-11-14 05:28:24 +01:00
|
|
|
|
{
|
|
|
|
|
// Get Biling Service
|
|
|
|
|
var billingService = BillingFactory.GetBillingService(config.BillingConfig);
|
|
|
|
|
|
2021-11-20 01:59:31 +01:00
|
|
|
|
// 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();
|
2021-11-20 01:59:31 +01:00
|
|
|
|
// 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));
|
2021-11-20 01:59:31 +01:00
|
|
|
|
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
|
|
|
|
{
|
2021-11-20 01:59:31 +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)
|
|
|
|
|
{
|
2021-11-20 01:59:31 +01:00
|
|
|
|
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)
|
|
|
|
|
{
|
2021-11-20 01:59:31 +01:00
|
|
|
|
BillingHelper.SetEmailLimits(config, user, emailPrice.Storage, true);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-12-01 05:33:34 +01:00
|
|
|
|
// Only reset their email size limit if they are premium
|
|
|
|
|
var userInfo = await IdentityHelper.GetIdentityUserInfo(config, user.Username);
|
|
|
|
|
if (userInfo != null &&
|
|
|
|
|
userInfo.AccountType == AccountType.Premium)
|
|
|
|
|
BillingHelper.SetEmailLimits(config, user, config.EmailConfig.MaxSize, 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|