yavsc/src/Yavsc.Server/Helpers/WorkflowHelpers.cs

112 lines
4.4 KiB
C#
Raw Normal View History

2017-01-31 13:19:12 +01:00
namespace Yavsc.Helpers
{
2017-02-03 15:25:47 +01:00
using System.Collections.Generic;
using System.Linq;
2023-03-19 17:57:55 +00:00
using Microsoft.EntityFrameworkCore;
2025-02-26 18:59:08 +00:00
using Yavsc.Abstract.Workflow;
using Yavsc.Billing;
2017-02-03 15:25:47 +01:00
using Yavsc.Models;
2025-02-26 18:59:08 +00:00
using Yavsc.Models.Billing;
using Yavsc.Models.Haircut;
using Yavsc.Models.Workflow;
2018-06-15 15:44:21 +02:00
using Yavsc.Services;
using Yavsc.ViewModels.FrontOffice;
public static class WorkflowHelpers
{
// Synchronization lock for billing service configuration
private static readonly object _billingLock = new object();
2025-07-14 18:58:04 +01:00
public static async Task<List<PerformerProfileViewModel>>
ListPerformersAsync(this ApplicationDbContext context,
2018-06-15 15:44:21 +02:00
IBillingService billing,
string actCode)
2017-02-03 15:25:47 +01:00
{
2025-07-14 18:58:04 +01:00
2018-06-15 15:44:21 +02:00
var actors = context.Performers
2025-07-14 18:58:04 +01:00
.Include(p => p.Activity)
.Include(p => p.Performer)
.Where(p => p.Active && p.Activity.Any(u => u.DoesCode == actCode)).OrderBy(x => x.Rate)
2018-06-15 15:44:21 +02:00
.ToArray();
2025-02-24 23:29:48 +00:00
2025-07-14 18:58:04 +01:00
List<PerformerProfileViewModel> result = new();
2025-02-24 23:29:48 +00:00
foreach (var a in actors)
{
var settings = await billing.GetPerformersSettingsAsync(actCode, a.PerformerId);
2025-07-14 18:58:04 +01:00
result.Add(new PerformerProfileViewModel(a, actCode, settings));
2025-02-24 23:29:48 +00:00
}
2018-06-15 15:44:21 +02:00
2019-08-20 16:49:38 +01:00
return result;
2017-02-03 15:25:47 +01:00
}
2017-05-12 16:29:47 +02:00
2025-07-14 18:58:04 +01:00
public static void RegisterBilling<T>(string code, Func<ApplicationDbContext, long,
2026-07-04 22:35:53 +01:00
IQuery> getter) where T : IBillable
2025-07-14 18:58:04 +01:00
{
lock (_billingLock)
2025-07-14 18:58:04 +01:00
{
string typeName = typeof(T).Name;
2026-04-19 20:36:03 +01:00
if (BillingService.GlobalBillingMap.ContainsKey(typeName))
{
2026-04-19 20:36:03 +01:00
throw new InvalidOperationException($"Billing setup: type '{typeName}' already registered with different code");
}
2026-04-19 20:36:03 +01:00
if (BillingService.Billing.ContainsKey(code))
{
2026-04-19 20:36:03 +01:00
throw new InvalidOperationException($"Billing setup: code '{code}' already registered with different type");
}
2026-04-19 20:36:03 +01:00
BillingService.Billing.Add(code, getter);
2025-07-14 18:58:04 +01:00
}
}
2025-02-26 18:59:08 +00:00
2025-07-14 18:58:04 +01:00
public static void ConfigureBillingService()
2025-02-26 18:59:08 +00:00
{
lock (_billingLock)
2025-02-26 18:59:08 +00:00
{
BillingService.Billing.Clear();
BillingService.GlobalBillingMap.Clear();
BillingService.UserSettings.Clear();
Config.ProfileTypes.Clear();
foreach (var a in System.AppDomain.CurrentDomain.GetAssemblies())
2025-02-26 18:59:08 +00:00
{
foreach (var c in a.GetTypes())
2025-07-14 18:58:04 +01:00
{
if (c.IsClass && !c.IsAbstract &&
2026-06-30 23:55:43 +01:00
c.GetInterface(nameof(IUserSettings)) != null)
{
Config.ProfileTypes.Add(c);
}
2025-07-14 18:58:04 +01:00
}
2025-02-26 18:59:08 +00:00
}
foreach (var propertyInfo in typeof(ApplicationDbContext).GetProperties())
2025-02-26 18:59:08 +00:00
{
2026-07-05 23:56:10 +01:00
if (propertyInfo.PropertyType.IsGenericType &&
propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>))
2025-07-14 18:58:04 +01:00
{
2026-07-05 23:56:10 +01:00
var entityType = propertyInfo.PropertyType.GetGenericArguments()[0];
if (typeof(IUserSettings).IsAssignableFrom(entityType))
{
BillingService.UserSettings.Add(propertyInfo);
}
2025-07-14 18:58:04 +01:00
}
2025-02-26 18:59:08 +00:00
}
2026-07-04 22:35:53 +01:00
RegisterBilling<HairCutQuery>(BillingCodes.Brush, new Func<ApplicationDbContext, long, IQuery>
((db, id) =>
{
2026-07-05 23:56:10 +01:00
var query = db.HairCutQueries.Include(q => q.Prestation).Include(q => q.Regularization).Single(q => q.Id == id);
query.SelectedProfile = db.BrusherProfile.Single(b => b.UserId == query.PerformerId);
return query;
}));
2025-02-26 18:59:08 +00:00
2026-07-04 22:35:53 +01:00
RegisterBilling<HairMultiCutQuery>(BillingCodes.MBrush, new Func<ApplicationDbContext, long, IQuery>
2026-07-05 23:56:10 +01:00
((db, id) => db.HairMultiCutQueries.Include(q => q.Regularization).Single(q => q.Id == id)));
2025-02-26 18:59:08 +00:00
2026-07-04 22:35:53 +01:00
RegisterBilling<RdvQuery>(BillingCodes.Rdv, new Func<ApplicationDbContext, long, IQuery>
2026-07-05 23:56:10 +01:00
((db, id) => db.RdvQueries.Include(q => q.Regularization).Single(q => q.Id == id)));
}
2025-07-14 18:58:04 +01:00
}
2025-02-26 18:59:08 +00:00
}
2017-05-12 16:29:47 +02:00
}