version-0.1 (#1)
Squash and merge of version-0.1 Co-authored-by: James Wampler <james@wamp.dev> Co-committed-by: James Wampler <james@wamp.dev>
This commit was merged in pull request #1.
This commit is contained in:
95
src/api/MicCheck.Api/Organizations/OrganizationService.cs
Normal file → Executable file
95
src/api/MicCheck.Api/Organizations/OrganizationService.cs
Normal file → Executable file
@@ -6,11 +6,16 @@ namespace MicCheck.Api.Organizations;
|
||||
|
||||
public class OrganizationService(MicCheckDbContext db, AuditService auditService)
|
||||
{
|
||||
public async Task<IReadOnlyList<Organization>> ListForUserAsync(int userId, CancellationToken ct = default)
|
||||
public async Task<IReadOnlyList<(Organization Org, bool IsPrimary)>> ListForUserAsync(int userId, CancellationToken ct = default)
|
||||
{
|
||||
return await db.Organizations
|
||||
.Where(o => o.Members.Any(m => m.UserId == userId))
|
||||
.ToListAsync(ct);
|
||||
var rows = await (
|
||||
from org in db.Organizations
|
||||
join member in db.OrganizationUsers on org.Id equals member.OrganizationId
|
||||
where member.UserId == userId
|
||||
select new { org, member.IsPrimary }
|
||||
).ToListAsync(ct);
|
||||
|
||||
return rows.Select(r => (r.org, r.IsPrimary)).ToList();
|
||||
}
|
||||
|
||||
public async Task<Organization?> FindByIdAsync(int id, CancellationToken ct = default)
|
||||
@@ -20,6 +25,8 @@ public class OrganizationService(MicCheckDbContext db, AuditService auditService
|
||||
|
||||
public async Task<Organization> CreateAsync(string name, int creatorUserId, CancellationToken ct = default)
|
||||
{
|
||||
var hasExistingOrg = await db.OrganizationUsers.AnyAsync(ou => ou.UserId == creatorUserId, ct);
|
||||
|
||||
var org = new Organization
|
||||
{
|
||||
Name = name,
|
||||
@@ -28,7 +35,8 @@ public class OrganizationService(MicCheckDbContext db, AuditService auditService
|
||||
org.Members.Add(new OrganizationUser
|
||||
{
|
||||
UserId = creatorUserId,
|
||||
Role = OrganizationRole.Admin
|
||||
Role = OrganizationRole.Admin,
|
||||
IsPrimary = !hasExistingOrg
|
||||
});
|
||||
db.Organizations.Add(org);
|
||||
await db.SaveChangesAsync(ct);
|
||||
@@ -38,6 +46,20 @@ public class OrganizationService(MicCheckDbContext db, AuditService auditService
|
||||
return org;
|
||||
}
|
||||
|
||||
public async Task SetPrimaryAsync(int organizationId, int userId, CancellationToken ct = default)
|
||||
{
|
||||
var member = await db.OrganizationUsers
|
||||
.FirstOrDefaultAsync(ou => ou.OrganizationId == organizationId && ou.UserId == userId, ct)
|
||||
?? throw new KeyNotFoundException($"User is not a member of organization {organizationId}.");
|
||||
|
||||
await db.OrganizationUsers
|
||||
.Where(ou => ou.UserId == userId && ou.IsPrimary)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(ou => ou.IsPrimary, false), ct);
|
||||
|
||||
member.IsPrimary = true;
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<Organization> UpdateAsync(int id, string name, CancellationToken ct = default)
|
||||
{
|
||||
var org = await db.Organizations.FirstOrDefaultAsync(o => o.Id == id, ct)
|
||||
@@ -100,4 +122,67 @@ public class OrganizationService(MicCheckDbContext db, AuditService auditService
|
||||
db.OrganizationUsers.Remove(member);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<string> GetOrCreateInviteTokenAsync(int organizationId, CancellationToken ct = default)
|
||||
{
|
||||
var org = await db.Organizations.FirstOrDefaultAsync(o => o.Id == organizationId, ct)
|
||||
?? throw new KeyNotFoundException($"Organization {organizationId} not found.");
|
||||
|
||||
if (org.InviteToken is null)
|
||||
{
|
||||
org.InviteToken = Guid.NewGuid().ToString("N");
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
return org.InviteToken;
|
||||
}
|
||||
|
||||
public async Task<string> RegenerateInviteTokenAsync(int organizationId, CancellationToken ct = default)
|
||||
{
|
||||
var org = await db.Organizations.FirstOrDefaultAsync(o => o.Id == organizationId, ct)
|
||||
?? throw new KeyNotFoundException($"Organization {organizationId} not found.");
|
||||
|
||||
org.InviteToken = Guid.NewGuid().ToString("N");
|
||||
await db.SaveChangesAsync(ct);
|
||||
return org.InviteToken;
|
||||
}
|
||||
|
||||
public async Task<Organization?> FindByInviteTokenAsync(string token, CancellationToken ct = default) =>
|
||||
await db.Organizations.FirstOrDefaultAsync(o => o.InviteToken == token, ct);
|
||||
|
||||
public async Task<IReadOnlyList<InviteByEmailResult>> InviteUsersByEmailAsync(int organizationId, IReadOnlyList<InviteByEmailEntry> invites, CancellationToken ct = default)
|
||||
{
|
||||
var results = new List<InviteByEmailResult>();
|
||||
|
||||
foreach (var invite in invites)
|
||||
{
|
||||
var user = await db.Users
|
||||
.FirstOrDefaultAsync(u => u.Email == invite.Email.Trim().ToLower() && u.IsActive, ct);
|
||||
|
||||
if (user is null)
|
||||
{
|
||||
results.Add(new InviteByEmailResult(invite.Email, false, "No user found with that email address."));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!Enum.TryParse<OrganizationRole>(invite.Role, ignoreCase: true, out var role))
|
||||
{
|
||||
results.Add(new InviteByEmailResult(invite.Email, false, $"Invalid role '{invite.Role}'."));
|
||||
continue;
|
||||
}
|
||||
|
||||
await InviteUserAsync(organizationId, user.Id, role, ct);
|
||||
results.Add(new InviteByEmailResult(invite.Email, true, null));
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
public async Task AcceptInviteAsync(string token, int userId, CancellationToken ct = default)
|
||||
{
|
||||
var org = await FindByInviteTokenAsync(token, ct)
|
||||
?? throw new KeyNotFoundException("Invalid or expired invite link.");
|
||||
|
||||
await InviteUserAsync(org.Id, userId, OrganizationRole.User, ct);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user