Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Mock numbers #798

Merged
merged 16 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/actions/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export enum Submit {
AuthPasswordHistoryUpdate = 'submit_auth_password_history_limit_update',
AuthPasswordDictionaryUpdate = 'submit_auth_password_dictionary_update',
AuthPersonalDataCheckUpdate = 'submit_auth_personal_data_check_update',
AuthMockNumbersUpdate = 'submit_auth_mock_numbers_update',
SessionsLengthUpdate = 'submit_sessions_length_update',
SessionsLimitUpdate = 'submit_sessions_limit_update',
SessionDelete = 'submit_session_delete',
Expand Down
3 changes: 2 additions & 1 deletion src/lib/elements/forms/inputPhone.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
autocomplete={autocomplete ? 'on' : 'off'}
bind:value
bind:this={element}
on:invalid={handleInvalid} />
on:invalid={handleInvalid}
on:input />
</div>
{#if error}
<Helper type="warning">{error}</Helper>
Expand Down
35 changes: 35 additions & 0 deletions src/lib/sdk/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { type Client, type Models } from '@appwrite.io/console';

export type MockNumber = {
phone: string;
otp: string;
};

export type Project = Models.Project & {
authMockNumbers: MockNumber[];
};

export class Auth {
client: Client;

constructor(client: Client) {
this.client = client;
}

async updateMockNumbers(projectId: string, numbers: MockNumber[]): Promise<Project> {
const path = `/projects/${projectId}/auth/mock-numbers`;
const params = {
projectId,
numbers: numbers
};
const uri = new URL(this.client.config.endpoint + path);
return await this.client.call(
'patch',
uri,
{
'content-type': 'application/json'
},
params
);
}
}
2 changes: 2 additions & 0 deletions src/lib/stores/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
Vcs
} from '@appwrite.io/console';
import { Billing } from '../sdk/billing';
import { Auth } from '$lib/sdk/auth';
import { Sources } from '$lib/sdk/sources';

const endpoint = VARS.APPWRITE_ENDPOINT ?? `${globalThis?.location?.origin}/v1`;
Expand Down Expand Up @@ -68,6 +69,7 @@ export const sdk = {
health: new Health(clientConsole),
locale: new Locale(clientConsole),
projects: new Projects(clientConsole),
auth: new Auth(clientConsole),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TorstenDittmann is there a way to combine this into the projects class ? Because that is where this method is supposed to be

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once the spec is generated and a sdk is released it will be automatically part of that 👍🏻

teams: new Teams(clientConsole),
users: new Users(clientConsole),
migrations: new Migrations(clientConsole),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { Container } from '$lib/layout';
import UpdateMockNumbers from './updateMockNumbers.svelte';
import UpdatePasswordDictionary from './updatePasswordDictionary.svelte';
import UpdatePasswordHistory from './updatePasswordHistory.svelte';
import UpdatePersonalDataCheck from './updatePersonalDataCheck.svelte';
Expand All @@ -10,6 +11,7 @@

<Container>
<UpdateUsersLimit />
<UpdateMockNumbers />
<UpdateSessionLength />
<UpdateSessionsLimit />
<UpdatePasswordHistory />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<script lang="ts">
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
import { CardGrid, Heading } from '$lib/components';
import { InputPhone, InputText } from '$lib/elements/forms';
import { Button, Form, FormItem, FormItemPart } from '$lib/elements/forms';
import type { MockNumber } from '$lib/sdk/auth';
import { sdk } from '$lib/stores/sdk';
import { project } from '../../store';
import { addNotification } from '$lib/stores/notifications';
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
import { writable } from 'svelte/store';
import Empty from '$lib/components/empty.svelte';

const numbers = writable<MockNumber[]>($project.authMockNumbers);
let initialNumbers = [];
let projectId: string = $project.$id;
let submitDisabled = true;

$: initialNumbers = $project.authMockNumbers.map((num) => ({ ...num }));
$: submitDisabled = JSON.stringify($numbers) === JSON.stringify(initialNumbers);
TorstenDittmann marked this conversation as resolved.
Show resolved Hide resolved

async function updateMockNumbers() {
try {
await sdk.forConsole.auth.updateMockNumbers(projectId, $numbers);
await invalidate(Dependencies.PROJECT);
addNotification({
type: 'success',
message: 'Updated mock phone numbers successfully'
});
trackEvent(Submit.AuthMockNumbersUpdate);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
trackError(error, Submit.AuthMockNumbersUpdate);
}
}

function addPhoneNumber(number: MockNumber) {
numbers.update((n) => [
...n,
{
phone: number.phone,
otp: number.otp
}
]);
}

function deletePhoneNumber(index: number) {
numbers.update((n) => {
n.splice(index, 1);
return n;
});
}
</script>

<Form onSubmit={updateMockNumbers}>
<CardGrid>
<Heading tag="h6" size="7" id="variables">Mock Phone Numbers</Heading>
<p>
Generate <b>fictional</b> numbers to simulate phone verification while testing demo accounts.
A maximum of 10 phone numbers can be generated.
</p>

<svelte:fragment slot="aside">
{#if $numbers.length > 0}
<ul class="form-list">
{#each $numbers as number, index}
<FormItem isMultiple>
<InputPhone
id={`key-${index}`}
bind:value={number.phone}
fullWidth
placeholder="Enter Phone Number"
label="Phone Number"
showLabel={index === 0 ? true : false}
maxlength={16}
required />
<InputText
id={`value-${index}`}
bind:value={number.otp}
fullWidth
placeholder="Enter value"
label="Verification Code"
showLabel={index === 0 ? true : false}
maxlength={6}
required />
<FormItemPart alignEnd>
<Button
text
disabled={$numbers.length === 0}
on:click={() => {
deletePhoneNumber(index);
}}>
<span class="icon-x" aria-hidden="true" />
</Button>
</FormItemPart>
</FormItem>
{/each}
</ul>
{#if $numbers.length < 10}
<Button
noMargin
text
on:click={() =>
addPhoneNumber({
phone: '',
otp: ''
})}
disabled={$numbers.length >= 10}>
<span class="icon-plus" aria-hidden="true" />
<span class="text">Add number</span>
</Button>
{/if}
{:else}
<Empty
on:click={() => {
addPhoneNumber({
phone: '',
otp: ''
});
}}>Create a mock phone number</Empty>
{/if}
</svelte:fragment>

<svelte:fragment slot="actions">
<Button disabled={submitDisabled} submit>Update</Button>
</svelte:fragment>
</CardGrid>
</Form>
4 changes: 2 additions & 2 deletions src/routes/console/project-[project]/store.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { page } from '$app/stores';
import type { Models } from '@appwrite.io/console';
import type { Project } from '$lib/sdk/auth';
import type { BarSeriesOption } from 'echarts/charts';
import { derived, writable } from 'svelte/store';

export const project = derived(
page,
($page) => $page.data.project as Models.Project & { region?: string }
($page) => $page.data.project as Project & { region?: string }
);
export const onboarding = derived(
project,
Expand Down
Loading