Exhaust key on usage

This commit is contained in:
JSH32 2022-09-21 10:53:59 -05:00
parent d91d044fd6
commit c4d4a8245c
3 changed files with 17 additions and 8 deletions

View File

@ -71,7 +71,7 @@ export const RegisterPrompt: React.FC = () => {
<FormLabel>Registration Code</FormLabel>
<Input {...register("registrationCode", { required: true })} />
</FormControl>
<Button type="submit">
<Button type="submit" colorScheme="primary">
Register
</Button>
</Stack>

View File

@ -43,7 +43,7 @@ export const SettingsLayout: React.FC<{
const userData = toJS(store?.userData)
return <Authenticated allowUnverified>
return <Authenticated allowUnverified allowUnregistered>
<Page title="Settings">
<Flex mt="7em" justify="center">
<Box w={{ base: "90vw", md: "700px" }}>

View File

@ -55,12 +55,21 @@ impl RegistrationKeyService {
/// Use a key once by its code.
pub async fn use_key(&self, code: &str) -> ServiceResult<()> {
let mut code = self.get_by_code(code).await?.into_active_model();
if let Set(Some(uses_left)) = code.uses_left {
code.uses_left = Set(Some(uses_left - 1));
code.update(self.database.as_ref())
.await
.map_err(|e| ServiceError::DbErr(e))?;
let code = self.get_by_code(code).await?;
if let Some(uses_left) = code.uses_left {
if uses_left - 1 <= 0 {
// Uses left has hit zero so we delete the code.
code.delete(self.database.as_ref())
.await
.map_err(|e| ServiceError::DbErr(e))?;
} else {
let mut active_code = code.into_active_model();
active_code.uses_left = Set(Some(uses_left - 1));
active_code
.update(self.database.as_ref())
.await
.map_err(|e| ServiceError::DbErr(e))?;
}
}
Ok(())