create isInviteValid helper

This commit is contained in:
jaina heartles 2022-12-09 22:38:04 -08:00
parent eb001b39a4
commit ee7ff9e69a
1 changed files with 7 additions and 2 deletions

View File

@ -396,6 +396,12 @@ fn ApiConn(comptime DbConn: type) type {
return try services.invites.get(self.db, invite_id, self.allocator);
}
fn isInviteValid(invite: services.invites.Invite) bool {
if (invite.max_uses != null and invite.times_used >= invite.max_uses.?) return false;
if (invite.expires_at != null and DateTime.now().isAfter(invite.expires_at.?)) return false;
return true;
}
pub fn register(self: *Self, username: []const u8, password: []const u8, opt: RegistrationOptions) !UserResponse {
const tx = try self.db.beginOrSavepoint();
const maybe_invite = if (opt.invite_code) |code|
@ -406,8 +412,7 @@ fn ApiConn(comptime DbConn: type) type {
if (maybe_invite) |invite| {
if (!Uuid.eql(invite.community_id, self.community.id)) return error.WrongCommunity;
if (invite.max_uses != null and invite.times_used >= invite.max_uses.?) return error.InviteExpired;
if (invite.expires_at != null and DateTime.now().isAfter(invite.expires_at.?)) return error.InviteExpired;
if (!isInviteValid(invite)) return error.InvalidInvite;
}
const invite_kind = if (maybe_invite) |inv| inv.kind else .user;