Skip to content

Commit

Permalink
fixes & super admin id
Browse files Browse the repository at this point in the history
  • Loading branch information
beliskner committed Apr 26, 2024
1 parent 5a172be commit 1dfea81
Show file tree
Hide file tree
Showing 14 changed files with 252 additions and 132 deletions.
34 changes: 10 additions & 24 deletions PatrickTheBot.AzureFunctions/Endpoints/Bucks/TransferFunds.cs
Original file line number Diff line number Diff line change
@@ -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<IActionResult> RunTransferFundsAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/transfer")] HttpRequest req,
[Table(WalletTableUtilities.TableName, Connection = "AzureWebJobsStorage")] TableClient walletTable,
ILogger log)
public async static Task<IActionResult> 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);
Expand Down Expand Up @@ -54,7 +39,10 @@ public static async Task<IActionResult> 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);
Expand All @@ -69,11 +57,9 @@ public static async Task<IActionResult> RunTransferFundsAsync([HttpTrigger(Autho
}

[FunctionName("CheckBalance")]
public static async Task<IActionResult> RunCheckBalanceAsync(
public async static Task<IActionResult> 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();

Expand All @@ -84,7 +70,7 @@ public static async Task<IActionResult> RunCheckBalanceAsync(

var userWallet = await WalletTableUtilities.GetWalletForSlackUser(user, walletTable, log);

if (userWallet.BackendBucks == 0)
if (userWallet.BackendBucks is 0)

Check warning on line 73 in PatrickTheBot.AzureFunctions/Endpoints/Bucks/TransferFunds.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Dereference of a possibly null reference.

Check warning on line 73 in PatrickTheBot.AzureFunctions/Endpoints/Bucks/TransferFunds.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Dereference of a possibly null reference.

Check warning on line 73 in PatrickTheBot.AzureFunctions/Endpoints/Bucks/TransferFunds.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

Dereference of a possibly null reference.
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()}"));
Expand Down
22 changes: 4 additions & 18 deletions PatrickTheBot.AzureFunctions/Endpoints/Points/AwardPoints.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -21,22 +7,22 @@ public static partial class PointsSystem
private const int MaxAwardAmount = 5;

[FunctionName("AwardBackendPoints")]
public static async Task<IActionResult> RunAwardBackendPointsAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/award-backend")] HttpRequest req,
public async static Task<IActionResult> RunAwardBackendPointsAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/award-backend")] HttpRequest req,
[Table(WalletTableUtilities.TableName, Connection = "AzureWebJobsStorage")] TableClient walletTable,
ILogger log)
{
return await BuildAwardResponse(req, walletTable, log, Department.Backend);
}

[FunctionName("AwardFrontendPoints")]
public static async Task<IActionResult> RunAwardFrontendPointsAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/award-frontend")] HttpRequest req,
public async static Task<IActionResult> 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<IActionResult> BuildAwardResponse(HttpRequest req, TableClient walletTable, ILogger log,
private async static Task<IActionResult> BuildAwardResponse(HttpRequest req, TableClient walletTable, ILogger log,
Department department)
{
await walletTable.CreateIfNotExistsAsync();
Expand Down
29 changes: 6 additions & 23 deletions PatrickTheBot.AzureFunctions/Endpoints/Points/GetPointsLevel.cs
Original file line number Diff line number Diff line change
@@ -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<IActionResult> RunGetBackendLevelAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/backend-level")] HttpRequest req,
public async static Task<IActionResult> RunGetBackendLevelAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/backend-level")] HttpRequest req,
[Table(WalletTableUtilities.TableName, Connection = "AzureWebJobsStorage")] TableClient walletTable,
ILogger log)
{
Expand All @@ -33,7 +21,7 @@ public static async Task<IActionResult> RunGetBackendLevelAsync([HttpTrigger(Aut
}

[FunctionName("GetFrontendLevel")]
public static async Task<IActionResult> RunGetFrontendLevelAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/frontend-level")] HttpRequest req,
public async static Task<IActionResult> RunGetFrontendLevelAsync([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = Route + "/frontend-level")] HttpRequest req,
[Table(WalletTableUtilities.TableName, Connection = "AzureWebJobsStorage")] TableClient walletTable,
ILogger log)
{
Expand All @@ -50,20 +38,15 @@ public static async Task<IActionResult> RunGetFrontendLevelAsync([HttpTrigger(Au
return string.IsNullOrWhiteSpace(levelString) ? new InternalServerErrorResult() : new OkObjectResult(new SlackResponse(levelString));
}

private static async Task<string?> ConstructLevelString(SlackUser user, TableClient walletTable, ILogger log, Department department)
private async static Task<string?> 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);
}
}
19 changes: 19 additions & 0 deletions PatrickTheBot.AzureFunctions/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 2 additions & 8 deletions PatrickTheBot.AzureFunctions/Models/SlackUser.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 1 addition & 5 deletions PatrickTheBot.AzureFunctions/Models/WalletTableEntity.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using Azure;
using Azure.Data.Tables;

namespace PatrickTheBot.AzureFunctions.Models;
namespace PatrickTheBot.AzureFunctions.Models;

public record WalletTableEntity : ITableEntity
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<RootNamespace>PatrickTheBot.AzureFunctions</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.6.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Tables" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="2.13.2" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Tables" Version="1.3.1" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.3.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
Expand Down
Loading

0 comments on commit 1dfea81

Please sign in to comment.