mirror of https://github.com/JSH32/Backpack.git
Exhaust key on usage
This commit is contained in:
parent
d91d044fd6
commit
c4d4a8245c
|
@ -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>
|
||||
|
|
|
@ -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" }}>
|
||||
|
|
|
@ -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(())
|
||||
|
|
Loading…
Reference in New Issue