From 1dfea8100c8afa66caaf8b48f684e2ba7d12b4d5 Mon Sep 17 00:00:00 2001 From: Beliskner Date: Fri, 26 Apr 2024 12:08:00 +0200 Subject: [PATCH] fixes & super admin id --- .../Endpoints/Bucks/TransferFunds.cs | 34 +--- .../Endpoints/Points/AwardPoints.cs | 22 +-- .../Endpoints/Points/GetPointsLevel.cs | 29 +-- PatrickTheBot.AzureFunctions/GlobalUsings.cs | 19 ++ .../Models/SlackUser.cs | 10 +- .../Models/WalletTableEntity.cs | 6 +- .../PatrickTheBot.AzureFunctions.csproj | 6 +- .../profile.arm.json | 173 ++++++++++++++++++ .../Resources/ExampleModels.cs | 5 +- .../Resources/UserIds.cs | 12 +- .../EnvironmentVariablesExtensions.cs | 8 + .../Utilities/SlackExtensions.cs | 37 ++-- .../Utilities/WalletExtensions.cs | 9 +- .../Utilities/WalletTableUtilities.cs | 14 +- 14 files changed, 252 insertions(+), 132 deletions(-) create mode 100644 PatrickTheBot.AzureFunctions/GlobalUsings.cs create mode 100644 PatrickTheBot.AzureFunctions/Properties/ServiceDependencies/patrickthebot - Zip Deploy/profile.arm.json create mode 100644 PatrickTheBot.AzureFunctions/Utilities/EnvironmentVariablesExtensions.cs diff --git a/PatrickTheBot.AzureFunctions/Endpoints/Bucks/TransferFunds.cs b/PatrickTheBot.AzureFunctions/Endpoints/Bucks/TransferFunds.cs index 45b2740..9ef4388 100644 --- a/PatrickTheBot.AzureFunctions/Endpoints/Bucks/TransferFunds.cs +++ b/PatrickTheBot.AzureFunctions/Endpoints/Bucks/TransferFunds.cs @@ -1,28 +1,13 @@ -using System.Linq; -using System.Threading.Tasks; -using System.Web.Http; -using Azure.Data.Tables; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Azure.WebJobs; -using Microsoft.Azure.WebJobs.Extensions.Http; -using Microsoft.Extensions.Logging; -using PatrickTheBot.AzureFunctions.Models; -using PatrickTheBot.AzureFunctions.Resources; -using PatrickTheBot.AzureFunctions.Utilities; - -namespace PatrickTheBot.AzureFunctions.Endpoints.Bucks; +namespace PatrickTheBot.AzureFunctions.Endpoints.Bucks; public static class TransferFunds { private const string Route = "wallet"; - private const string CurrencyName = "Backend Bucks"; [FunctionName("TransferFunds")] - public static async Task RunTransferFundsAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/transfer")] HttpRequest req, - [Table(WalletTableUtilities.TableName, Connection = "AzureWebJobsStorage")] TableClient walletTable, - ILogger log) + public async static Task RunTransferFundsAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/transfer")] HttpRequest req, + [Table(WalletTableUtilities.TableName, Connection = "AzureWebJobsStorage")] TableClient walletTable, ILogger log) { await walletTable.CreateIfNotExistsAsync(); var canFindText = req.Form.TryGetValue("text", out var textFormValue); @@ -54,7 +39,10 @@ public static async Task RunTransferFundsAsync([HttpTrigger(Autho var senderWallet = senderWalletTask.Result; var recipientWallet = recipientWalletTask.Result; - if (senderWallet.BackendBucks - transferAmount < 0 && !UserIds.BackendAdminIds.Contains(sender.Id)) + if (senderWallet is null || recipientWallet is null) + return new OkObjectResult(new SlackResponse("Come on buddy, to be able to transfer money both of you gotta have wallets. Get your act together.")); + + if (senderWallet.BackendBucks - transferAmount < 0 && !UserIds.IsSuperAdmin(sender.Id)) return new OkObjectResult(new SlackResponse($"Seriously? Transferring {transferAmount} {CurrencyName} would put you in debt. Did you really think we do loans here?")); var transferSuccessful = await WalletTableUtilities.TransferBackendBucks(senderWallet, recipientWallet, transferAmount, walletTable, log); @@ -69,11 +57,9 @@ public static async Task RunTransferFundsAsync([HttpTrigger(Autho } [FunctionName("CheckBalance")] - public static async Task RunCheckBalanceAsync( + public async static Task RunCheckBalanceAsync( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/balance")] HttpRequest req, - [Table(WalletTableUtilities.TableName, Connection = "AzureWebJobsStorage")] - TableClient walletTable, - ILogger log) + [Table(WalletTableUtilities.TableName, Connection = "AzureWebJobsStorage")] TableClient walletTable, ILogger log) { await walletTable.CreateIfNotExistsAsync(); @@ -84,7 +70,7 @@ public static async Task RunCheckBalanceAsync( var userWallet = await WalletTableUtilities.GetWalletForSlackUser(user, walletTable, log); - if (userWallet.BackendBucks == 0) + if (userWallet.BackendBucks is 0) return new OkObjectResult(new SlackResponse("Your broke ass is in possession of a grand total of 💲0 buckeroonies")); return new OkObjectResult(new SlackResponse($"Your current balance is 💲{userWallet.BackendBucks} {CurrencyName} {userWallet.PrintAlternativeCurrencies()}")); diff --git a/PatrickTheBot.AzureFunctions/Endpoints/Points/AwardPoints.cs b/PatrickTheBot.AzureFunctions/Endpoints/Points/AwardPoints.cs index d6caf76..7ed9fc8 100644 --- a/PatrickTheBot.AzureFunctions/Endpoints/Points/AwardPoints.cs +++ b/PatrickTheBot.AzureFunctions/Endpoints/Points/AwardPoints.cs @@ -1,18 +1,4 @@ -using System.Threading.Tasks; -using System.Web.Http; -using Azure; -using Azure.Data.Tables; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Azure.WebJobs; -using Microsoft.Azure.WebJobs.Extensions.Http; -using Microsoft.Extensions.Logging; -using PatrickTheBot.AzureFunctions.Enums; -using PatrickTheBot.AzureFunctions.Models; -using PatrickTheBot.AzureFunctions.Resources; -using PatrickTheBot.AzureFunctions.Utilities; - -namespace PatrickTheBot.AzureFunctions.Endpoints.Points; +namespace PatrickTheBot.AzureFunctions.Endpoints.Points; public static partial class PointsSystem { @@ -21,7 +7,7 @@ public static partial class PointsSystem private const int MaxAwardAmount = 5; [FunctionName("AwardBackendPoints")] - public static async Task RunAwardBackendPointsAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/award-backend")] HttpRequest req, + public async static Task RunAwardBackendPointsAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/award-backend")] HttpRequest req, [Table(WalletTableUtilities.TableName, Connection = "AzureWebJobsStorage")] TableClient walletTable, ILogger log) { @@ -29,14 +15,14 @@ public static async Task RunAwardBackendPointsAsync([HttpTrigger( } [FunctionName("AwardFrontendPoints")] - public static async Task RunAwardFrontendPointsAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/award-frontend")] HttpRequest req, + public async static Task RunAwardFrontendPointsAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/award-frontend")] HttpRequest req, [Table(WalletTableUtilities.TableName, Connection = "AzureWebJobsStorage")] TableClient walletTable, ILogger log) { return await BuildAwardResponse(req, walletTable, log, Department.Frontend); } - private static async Task BuildAwardResponse(HttpRequest req, TableClient walletTable, ILogger log, + private async static Task BuildAwardResponse(HttpRequest req, TableClient walletTable, ILogger log, Department department) { await walletTable.CreateIfNotExistsAsync(); diff --git a/PatrickTheBot.AzureFunctions/Endpoints/Points/GetPointsLevel.cs b/PatrickTheBot.AzureFunctions/Endpoints/Points/GetPointsLevel.cs index f6e4369..297187a 100644 --- a/PatrickTheBot.AzureFunctions/Endpoints/Points/GetPointsLevel.cs +++ b/PatrickTheBot.AzureFunctions/Endpoints/Points/GetPointsLevel.cs @@ -1,21 +1,9 @@ -using System.Threading.Tasks; -using System.Web.Http; -using Azure.Data.Tables; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Azure.WebJobs; -using Microsoft.Azure.WebJobs.Extensions.Http; -using Microsoft.Extensions.Logging; -using PatrickTheBot.AzureFunctions.Enums; -using PatrickTheBot.AzureFunctions.Models; -using PatrickTheBot.AzureFunctions.Utilities; - -namespace PatrickTheBot.AzureFunctions.Endpoints.Points; +namespace PatrickTheBot.AzureFunctions.Endpoints.Points; public static partial class PointsSystem { [FunctionName("GetBackendLevel")] - public static async Task RunGetBackendLevelAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/backend-level")] HttpRequest req, + public async static Task RunGetBackendLevelAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/backend-level")] HttpRequest req, [Table(WalletTableUtilities.TableName, Connection = "AzureWebJobsStorage")] TableClient walletTable, ILogger log) { @@ -33,7 +21,7 @@ public static async Task RunGetBackendLevelAsync([HttpTrigger(Aut } [FunctionName("GetFrontendLevel")] - public static async Task RunGetFrontendLevelAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/frontend-level")] HttpRequest req, + public async static Task RunGetFrontendLevelAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/frontend-level")] HttpRequest req, [Table(WalletTableUtilities.TableName, Connection = "AzureWebJobsStorage")] TableClient walletTable, ILogger log) { @@ -50,20 +38,15 @@ public static async Task RunGetFrontendLevelAsync([HttpTrigger(Au return string.IsNullOrWhiteSpace(levelString) ? new InternalServerErrorResult() : new OkObjectResult(new SlackResponse(levelString)); } - private static async Task ConstructLevelString(SlackUser user, TableClient walletTable, ILogger log, Department department) + private async static Task ConstructLevelString(SlackUser user, TableClient walletTable, ILogger log, Department department) { await walletTable.CreateIfNotExistsAsync(); var wallet = await WalletTableUtilities.GetWalletForSlackUser(user, walletTable, log); - if (wallet is null) + if (wallet is null || department is Department.Other) return null; - return department switch - { - Department.Backend => $"You're currently {wallet.ToPointsBackendLevel()}", - Department.Frontend => $"You're currently {wallet.ToPointsFrontendLevel()}", - _ => null - }; + return wallet.ToPointsLevel(department); } } \ No newline at end of file diff --git a/PatrickTheBot.AzureFunctions/GlobalUsings.cs b/PatrickTheBot.AzureFunctions/GlobalUsings.cs new file mode 100644 index 0000000..4a4a8b8 --- /dev/null +++ b/PatrickTheBot.AzureFunctions/GlobalUsings.cs @@ -0,0 +1,19 @@ +// Global using directives +global using System; +global using System.Collections.Generic; +global using System.Linq; +global using System.Text; +global using System.Threading.Tasks; +global using System.Web.Http; +global using Azure; +global using Azure.Data.Tables; +global using Microsoft.AspNetCore.Http; +global using Microsoft.AspNetCore.Mvc; +global using Microsoft.Azure.WebJobs; +global using Microsoft.Azure.WebJobs.Extensions.Http; +global using Microsoft.Extensions.Logging; +global using PatrickTheBot.AzureFunctions.Endpoints.Points; +global using PatrickTheBot.AzureFunctions.Enums; +global using PatrickTheBot.AzureFunctions.Models; +global using PatrickTheBot.AzureFunctions.Resources; +global using PatrickTheBot.AzureFunctions.Utilities; \ No newline at end of file diff --git a/PatrickTheBot.AzureFunctions/Models/SlackUser.cs b/PatrickTheBot.AzureFunctions/Models/SlackUser.cs index 4e63a10..7ccda56 100644 --- a/PatrickTheBot.AzureFunctions/Models/SlackUser.cs +++ b/PatrickTheBot.AzureFunctions/Models/SlackUser.cs @@ -1,10 +1,4 @@ -using System.Linq; -using Microsoft.AspNetCore.Http; -using PatrickTheBot.AzureFunctions.Endpoints.Points; -using PatrickTheBot.AzureFunctions.Enums; -using PatrickTheBot.AzureFunctions.Resources; - -namespace PatrickTheBot.AzureFunctions.Models; +namespace PatrickTheBot.AzureFunctions.Models; public record SlackUser { @@ -64,7 +58,7 @@ public static bool IsAuthorizedForCommand(SlackUser slackUser, Department depart private static Department GetDepartment(string userId) { - if (UserIds.BackendAdminIds.Contains(userId) || UserIds.BackendInternIds.Contains(userId)) + if (UserIds.IsSuperAdmin(userId) || UserIds.BackendAdminIds.Contains(userId) || UserIds.BackendInternIds.Contains(userId)) return Department.Backend; if (UserIds.FrontendAdminIds.Contains(userId)) return Department.Frontend; diff --git a/PatrickTheBot.AzureFunctions/Models/WalletTableEntity.cs b/PatrickTheBot.AzureFunctions/Models/WalletTableEntity.cs index abf1610..25137e5 100644 --- a/PatrickTheBot.AzureFunctions/Models/WalletTableEntity.cs +++ b/PatrickTheBot.AzureFunctions/Models/WalletTableEntity.cs @@ -1,8 +1,4 @@ -using System; -using Azure; -using Azure.Data.Tables; - -namespace PatrickTheBot.AzureFunctions.Models; +namespace PatrickTheBot.AzureFunctions.Models; public record WalletTableEntity : ITableEntity { diff --git a/PatrickTheBot.AzureFunctions/PatrickTheBot.AzureFunctions.csproj b/PatrickTheBot.AzureFunctions/PatrickTheBot.AzureFunctions.csproj index 1060cc5..7a5e22d 100644 --- a/PatrickTheBot.AzureFunctions/PatrickTheBot.AzureFunctions.csproj +++ b/PatrickTheBot.AzureFunctions/PatrickTheBot.AzureFunctions.csproj @@ -6,9 +6,9 @@ PatrickTheBot.AzureFunctions - - - + + + diff --git a/PatrickTheBot.AzureFunctions/Properties/ServiceDependencies/patrickthebot - Zip Deploy/profile.arm.json b/PatrickTheBot.AzureFunctions/Properties/ServiceDependencies/patrickthebot - Zip Deploy/profile.arm.json new file mode 100644 index 0000000..3986e01 --- /dev/null +++ b/PatrickTheBot.AzureFunctions/Properties/ServiceDependencies/patrickthebot - Zip Deploy/profile.arm.json @@ -0,0 +1,173 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "metadata": { + "_dependencyType": "compute.function.linux.appService" + }, + "parameters": { + "resourceGroupName": { + "type": "string", + "defaultValue": "BotBusiness", + "metadata": { + "description": "Name of the resource group for the resource. It is recommended to put resources under same resource group for better tracking." + } + }, + "resourceGroupLocation": { + "type": "string", + "defaultValue": "westeurope", + "metadata": { + "description": "Location of the resource group. Resource groups could have different location than resources, however by default we use API versions from latest hybrid profile which support all locations for resource types we support." + } + }, + "resourceName": { + "type": "string", + "defaultValue": "patrickthebot", + "metadata": { + "description": "Name of the main resource to be created by this template." + } + }, + "resourceLocation": { + "type": "string", + "defaultValue": "[parameters('resourceGroupLocation')]", + "metadata": { + "description": "Location of the resource. By default use resource group's location, unless the resource provider is not supported there." + } + } + }, + "resources": [ + { + "type": "Microsoft.Resources/resourceGroups", + "name": "[parameters('resourceGroupName')]", + "location": "[parameters('resourceGroupLocation')]", + "apiVersion": "2019-10-01" + }, + { + "type": "Microsoft.Resources/deployments", + "name": "[concat(parameters('resourceGroupName'), 'Deployment', uniqueString(concat(parameters('resourceName'), subscription().subscriptionId)))]", + "resourceGroup": "[parameters('resourceGroupName')]", + "apiVersion": "2019-10-01", + "dependsOn": [ + "[parameters('resourceGroupName')]" + ], + "properties": { + "mode": "Incremental", + "expressionEvaluationOptions": { + "scope": "inner" + }, + "parameters": { + "resourceGroupName": { + "value": "[parameters('resourceGroupName')]" + }, + "resourceGroupLocation": { + "value": "[parameters('resourceGroupLocation')]" + }, + "resourceName": { + "value": "[parameters('resourceName')]" + }, + "resourceLocation": { + "value": "[parameters('resourceLocation')]" + } + }, + "template": { + "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "resourceGroupName": { + "type": "string" + }, + "resourceGroupLocation": { + "type": "string" + }, + "resourceName": { + "type": "string" + }, + "resourceLocation": { + "type": "string" + } + }, + "variables": { + "storage_name": "[toLower(concat('storage', uniqueString(concat(parameters('resourceName'), subscription().subscriptionId))))]", + "appServicePlan_name": "[concat('Plan', uniqueString(concat(parameters('resourceName'), subscription().subscriptionId)))]", + "storage_ResourceId": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/', parameters('resourceGroupName'), '/providers/Microsoft.Storage/storageAccounts/', variables('storage_name'))]", + "appServicePlan_ResourceId": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/', parameters('resourceGroupName'), '/providers/Microsoft.Web/serverFarms/', variables('appServicePlan_name'))]", + "function_ResourceId": "[concat('/subscriptions/', subscription().subscriptionId, '/resourceGroups/', parameters('resourceGroupName'), '/providers/Microsoft.Web/sites/', parameters('resourceName'))]" + }, + "resources": [ + { + "location": "[parameters('resourceLocation')]", + "name": "[parameters('resourceName')]", + "type": "Microsoft.Web/sites", + "apiVersion": "2015-08-01", + "tags": { + "[concat('hidden-related:', variables('appServicePlan_ResourceId'))]": "empty" + }, + "dependsOn": [ + "[variables('appServicePlan_ResourceId')]", + "[variables('storage_ResourceId')]" + ], + "kind": "functionapp", + "properties": { + "name": "[parameters('resourceName')]", + "kind": "functionapp", + "httpsOnly": true, + "reserved": false, + "serverFarmId": "[variables('appServicePlan_ResourceId')]", + "siteConfig": { + "alwaysOn": true, + "linuxFxVersion": "dotnet|3.1" + } + }, + "identity": { + "type": "SystemAssigned" + }, + "resources": [ + { + "name": "appsettings", + "type": "config", + "apiVersion": "2015-08-01", + "dependsOn": [ + "[variables('function_ResourceId')]" + ], + "properties": { + "AzureWebJobsStorage": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storage_name'), ';AccountKey=', listKeys(variables('storage_ResourceId'), '2017-10-01').keys[0].value, ';EndpointSuffix=', 'core.windows.net')]", + "FUNCTIONS_EXTENSION_VERSION": "~3", + "FUNCTIONS_WORKER_RUNTIME": "dotnet" + } + } + ] + }, + { + "location": "[parameters('resourceGroupLocation')]", + "name": "[variables('storage_name')]", + "type": "Microsoft.Storage/storageAccounts", + "apiVersion": "2017-10-01", + "tags": { + "[concat('hidden-related:', concat('/providers/Microsoft.Web/sites/', parameters('resourceName')))]": "empty" + }, + "properties": { + "supportsHttpsTrafficOnly": true + }, + "sku": { + "name": "Standard_LRS" + }, + "kind": "Storage" + }, + { + "location": "[parameters('resourceGroupLocation')]", + "name": "[variables('appServicePlan_name')]", + "type": "Microsoft.Web/serverFarms", + "apiVersion": "2015-02-01", + "kind": "linux", + "properties": { + "name": "[variables('appServicePlan_name')]", + "sku": "Standard", + "workerSizeId": "0", + "reserved": true + } + } + ] + } + } + } + ] +} \ No newline at end of file diff --git a/PatrickTheBot.AzureFunctions/Resources/ExampleModels.cs b/PatrickTheBot.AzureFunctions/Resources/ExampleModels.cs index 58ca64c..49c7c60 100644 --- a/PatrickTheBot.AzureFunctions/Resources/ExampleModels.cs +++ b/PatrickTheBot.AzureFunctions/Resources/ExampleModels.cs @@ -1,7 +1,4 @@ -using PatrickTheBot.AzureFunctions.Models; -using PatrickTheBot.AzureFunctions.Utilities; - -namespace PatrickTheBot.AzureFunctions.Resources; +namespace PatrickTheBot.AzureFunctions.Resources; public static class ExampleModels { diff --git a/PatrickTheBot.AzureFunctions/Resources/UserIds.cs b/PatrickTheBot.AzureFunctions/Resources/UserIds.cs index 8ee2570..ecd8e20 100644 --- a/PatrickTheBot.AzureFunctions/Resources/UserIds.cs +++ b/PatrickTheBot.AzureFunctions/Resources/UserIds.cs @@ -11,17 +11,19 @@ public static class UserIds public static readonly string[] BackendAdminIds = { - "D070JQA3FAR", - "00000000006", + "00000000003", "00000000004", - "00000000005" + "00000000005", + "00000000006" }; public static readonly string[] FrontendAdminIds = { - "D070JQA3FAR", + "00000000007", "00000000008", - "00000000010", "00000000009", + "00000000010", }; + + public static bool IsSuperAdmin(string? userId) => EnvironmentVariablesExtensions.SuperAdminEnvironmentVariableId is { Length: > 0 } superAdminId && string.Equals(userId, superAdminId); } \ No newline at end of file diff --git a/PatrickTheBot.AzureFunctions/Utilities/EnvironmentVariablesExtensions.cs b/PatrickTheBot.AzureFunctions/Utilities/EnvironmentVariablesExtensions.cs new file mode 100644 index 0000000..c25a2d4 --- /dev/null +++ b/PatrickTheBot.AzureFunctions/Utilities/EnvironmentVariablesExtensions.cs @@ -0,0 +1,8 @@ +namespace PatrickTheBot.AzureFunctions.Utilities; + +public static class EnvironmentVariablesExtensions +{ + private static string GetEnvironmentVariable(string name) => name + ": " + Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process); + + public static string SuperAdminEnvironmentVariableId => GetEnvironmentVariable("SuperAdmin"); +} \ No newline at end of file diff --git a/PatrickTheBot.AzureFunctions/Utilities/SlackExtensions.cs b/PatrickTheBot.AzureFunctions/Utilities/SlackExtensions.cs index 2e39fd9..af1795b 100644 --- a/PatrickTheBot.AzureFunctions/Utilities/SlackExtensions.cs +++ b/PatrickTheBot.AzureFunctions/Utilities/SlackExtensions.cs @@ -1,7 +1,4 @@ -using PatrickTheBot.AzureFunctions.Endpoints.Points; -using PatrickTheBot.AzureFunctions.Models; - -namespace PatrickTheBot.AzureFunctions.Utilities; +namespace PatrickTheBot.AzureFunctions.Utilities; public static class SlackExtensions { @@ -24,25 +21,15 @@ public static class SlackExtensions // This default can't possibly go wrong, can it? 🤡 _ => "a Frontend Developer" }; - - public static string ToPointsBackendLevel(this WalletTableEntity walletTableEntity) => walletTableEntity.BackendPoints switch - { - > 0 and <= 25 => "a human being with a set of hands and a heartbeat is our best guess", - > 25 and <= 50 => "a Backend lackey, get back to work slacker", - > 50 and <= 75 => "a baby Backend developer, aren't you cute!", - > 75 and <= 100 => "an aspiring honorary Backend developer, keep trying your best little code monkey", - > 100 => "an honorary Backend developer, we're proud of you colleague!", - _ => $"a master Backend developer! Psyche! You haven't scored any {PointsSystem.BackendPointsName} so you tell me what that makes you" - }; - - public static string ToPointsFrontendLevel(this WalletTableEntity walletTableEntity) => walletTableEntity.FrontendPoints switch - { - // TODO: Change these levels or just flatten the function together with the BackendLevel function - > 0 and <= 25 => "a human being with a set of hands and a heartbeat is our best guess", - > 25 and <= 50 => "a Frontend lackey, get back to work slacker", - > 50 and <= 75 => "a baby Frontend developer, aren't you cute!", - > 75 and <= 100 => "an aspiring honorary Frontend developer, keep trying your best little code monkey", - > 100 => "an honorary Frontend developer, we're proud of you colleague!", - _ => $"a master Frontend developer! Psyche! You haven't scored any {PointsSystem.FrontendPointsName} so you tell me what that makes you" - }; + + public static string ToPointsLevel(this WalletTableEntity walletTableEntity, Department department) + => (department is Department.Backend ? walletTableEntity.BackendPoints : walletTableEntity.FrontendPoints) switch + { + > 0 and <= 25 => "a human being with a set of hands and a heartbeat is our best guess", + > 25 and <= 50 => "a Frontend lackey, get back to work slacker", + > 50 and <= 75 => "a baby Frontend developer, aren't you cute!", + > 75 and <= 100 => "an aspiring honorary Frontend developer, keep trying your best little code monkey", + > 100 => "an honorary Frontend developer, we're proud of you colleague!", + _ => $"a master Frontend developer! Psyche! You haven't scored any {PointsSystem.FrontendPointsName} so you tell me what that makes you" + }; } \ No newline at end of file diff --git a/PatrickTheBot.AzureFunctions/Utilities/WalletExtensions.cs b/PatrickTheBot.AzureFunctions/Utilities/WalletExtensions.cs index 29a5680..9d6b8e6 100644 --- a/PatrickTheBot.AzureFunctions/Utilities/WalletExtensions.cs +++ b/PatrickTheBot.AzureFunctions/Utilities/WalletExtensions.cs @@ -1,9 +1,4 @@ -using System.Collections.Generic; -using System.Linq; -using System.Text; -using PatrickTheBot.AzureFunctions.Models; - -namespace PatrickTheBot.AzureFunctions.Utilities; +namespace PatrickTheBot.AzureFunctions.Utilities; public static class WalletExtensions { @@ -71,7 +66,7 @@ private static (int, Dictionary) TryAddAlternativeCurrencyAndReturn { var amount = remainder / currencyValue; if (amount <= 0) return (remainder, altDictionary); - altDictionary.Add(amount == 1 ? currencyName : $"{currencyName}s", amount); + altDictionary.Add(amount is 1 ? currencyName : $"{currencyName}s", amount); return (remainder % currencyValue, altDictionary); } diff --git a/PatrickTheBot.AzureFunctions/Utilities/WalletTableUtilities.cs b/PatrickTheBot.AzureFunctions/Utilities/WalletTableUtilities.cs index b9d9f49..cf37f13 100644 --- a/PatrickTheBot.AzureFunctions/Utilities/WalletTableUtilities.cs +++ b/PatrickTheBot.AzureFunctions/Utilities/WalletTableUtilities.cs @@ -1,17 +1,11 @@ -using System.Threading.Tasks; -using Azure; -using Azure.Data.Tables; -using Microsoft.Extensions.Logging; -using PatrickTheBot.AzureFunctions.Models; - -namespace PatrickTheBot.AzureFunctions.Utilities; +namespace PatrickTheBot.AzureFunctions.Utilities; public static class WalletTableUtilities { public const string TableName = "wallets"; private const string PartitionKey = "WALLET"; - public static async Task TransferBackendBucks(WalletTableEntity senderWallet, WalletTableEntity recipientWallet, + public async static Task TransferBackendBucks(WalletTableEntity senderWallet, WalletTableEntity recipientWallet, int amount, TableClient walletTable, ILogger log) { var transferSucceeded = true; @@ -38,7 +32,7 @@ public static async Task TransferBackendBucks(WalletTableEntity senderWall return transferSucceeded; } - public static async Task GetWalletForSlackUser(SlackUser slackUser, TableClient walletTable, ILogger log) + public async static Task GetWalletForSlackUser(SlackUser slackUser, TableClient walletTable, ILogger log) { log.LogInformation("Getting wallet by id {Id}", slackUser.Id); WalletTableEntity wallet; @@ -56,7 +50,7 @@ public static async Task TransferBackendBucks(WalletTableEntity senderWall return wallet; } - private static async Task CreateWalletForSlackUser(SlackUser slackUser, TableClient walletTable, ILogger log) + private async static Task CreateWalletForSlackUser(SlackUser slackUser, TableClient walletTable, ILogger log) { var wallet = new Wallet(slackUser).ToTable(); await walletTable.AddEntityAsync(wallet);