Skip to content

Commit 3a85a19

Browse files
committed
feat: ✨ Use nano ids
1 parent 5f3fc4b commit 3a85a19

File tree

7 files changed

+135
-92
lines changed

7 files changed

+135
-92
lines changed

apps/app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@
6363
"pnpm": ">=8"
6464
},
6565
"dependencies": {
66-
"@invoicelink/db": "workspace:*",
6766
"@floating-ui/dom": "^1.6.8",
67+
"@invoicelink/db": "workspace:*",
6868
"@lucia-auth/adapter-prisma": "4.0.1",
6969
"@paralleldrive/cuid2": "^2.2.2",
7070
"@prisma/client": "5.17.0",
@@ -77,6 +77,7 @@
7777
"javascript-time-ago": "^2.5.10",
7878
"jwt-decode": "^4.0.0",
7979
"lucia": "3.2.0",
80+
"nanoid": "^5.0.7",
8081
"oslo": "^1.2.1",
8182
"posthog-js": "^1.154.2",
8283
"prisma": "5.17.0",

apps/app/src/lib/utils/id.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { customAlphabet } from 'nanoid';
2+
3+
const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
4+
5+
export const generateId = customAlphabet(alphabet, 9);

apps/app/src/routes/(app)/invoices/management/manage/+page.server.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createCheckout } from '$lib/utils/yoco';
77
import { SerialType } from '@invoicelink/db';
88
import { getNextSerial } from '$lib/utils/serialNumbers';
99
import { schema } from './validation';
10+
import { generateId } from '$lib/utils/id';
1011

1112
export const load = (async ({ parent, locals, url }) => {
1213
await parent();
@@ -149,6 +150,7 @@ export const actions: Actions = {
149150
if (user?.id) {
150151
let invoice = await prisma.invoice.create({
151152
data: {
153+
id: generateId(),
152154
issueDate: form.data.issueDate,
153155
dueDate: form.data.dueDate,
154156
description: form.data.description,
@@ -180,9 +182,9 @@ export const actions: Actions = {
180182
const { errors: yocoErrors, checkout: yocoCheckout } = await createCheckout({
181183
secretKey: yocoIntegration.secretKey,
182184
amount: form.data.total,
183-
cancelUrl: `${url.origin}/pay?id=${invoice.id}`,
184-
failureUrl: `${url.origin}/pay?id=${invoice.id}`,
185-
successUrl: `${url.origin}/pay?id=${invoice.id}`
185+
cancelUrl: `${url.origin}/${invoice.id}`,
186+
failureUrl: `${url.origin}/${invoice.id}`,
187+
successUrl: `${url.origin}/${invoice.id}`
186188
});
187189

188190
if (yocoErrors) {
@@ -336,9 +338,9 @@ export const actions: Actions = {
336338
const { errors: yocoErrors, checkout: yocoCheckout } = await createCheckout({
337339
secretKey: yocoIntegration.secretKey,
338340
amount: updatedInvoice.total,
339-
cancelUrl: `${url.origin}/pay?id=${updatedInvoice.id}`,
340-
failureUrl: `${url.origin}/pay?id=${updatedInvoice.id}`,
341-
successUrl: `${url.origin}/pay?id=${updatedInvoice.id}`
341+
cancelUrl: `${url.origin}/${updatedInvoice.id}`,
342+
failureUrl: `${url.origin}/${updatedInvoice.id}`,
343+
successUrl: `${url.origin}/${updatedInvoice.id}`
342344
});
343345

344346
if (yocoErrors) {

apps/app/src/routes/(app)/invoices/management/manage/+page.svelte

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<script lang="ts">
22
import type { PageData } from './$types';
33
export let data: PageData;
4-
import { page } from '$app/stores';
54
65
import Invoice from '$lib/components/Invoice.svelte';
76
import PageHeading from '$lib/components/PageHeading.svelte';
@@ -88,6 +87,9 @@
8887
address: defaultAddress
8988
};
9089
}
90+
91+
// payment link
92+
const paymentLink = `https://pay.invoicelink.io/${$form.id}`;
9193
</script>
9294

9395
<PageHeading>
@@ -98,7 +100,7 @@
98100
class="tooltip tooltip-accent"
99101
data-tip="Preview invoicelink"
100102
target="_blank"
101-
href="/pay?id={$form.id}"
103+
href={paymentLink}
102104
>
103105
<Icon name="launch" />
104106
</a>
@@ -110,10 +112,7 @@
110112
>
111113
<Icon name="download" />
112114
</a>
113-
<CopyToClipboard
114-
text={`${$page.url.origin}/pay?id=${$form.id}`}
115-
message="Invoicelink copied"
116-
/>
115+
<CopyToClipboard text={paymentLink} message="Invoicelink copied" />
117116
</div>
118117
{/if}
119118
</PageHeading>

apps/app/src/routes/(app)/quick-links/+page.server.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { SerialType } from '@invoicelink/db';
77
import { incrementSerialNumber } from '$lib/utils/serialNumbers';
88
import { createCheckout } from '$lib/utils/yoco';
99
import { quickLinkSchema } from '../validation';
10+
import { generateId } from '$lib/utils/id';
1011

1112
export const load = (async ({ parent, locals }) => {
1213
await parent();
@@ -93,6 +94,7 @@ export const actions: Actions = {
9394
if (user?.id) {
9495
let quickLink = await prisma.quickLink.create({
9596
data: {
97+
id: generateId(),
9698
subtotal: form.data.amount,
9799
tax: 0,
98100
total: form.data.amount,
@@ -117,9 +119,9 @@ export const actions: Actions = {
117119
const { errors: yocoErrors, checkout: yocoCheckout } = await createCheckout({
118120
secretKey: yocoIntegration.secretKey,
119121
amount: form.data.amount,
120-
cancelUrl: `${url.origin}/pay?id=${quickLink.id}`,
121-
failureUrl: `${url.origin}/pay?id=${quickLink.id}`,
122-
successUrl: `${url.origin}/pay?id=${quickLink.id}`
122+
cancelUrl: `${url.origin}/${quickLink.id}`,
123+
failureUrl: `${url.origin}/${quickLink.id}`,
124+
successUrl: `${url.origin}/${quickLink.id}`
123125
});
124126

125127
if (yocoErrors) {

apps/app/src/routes/(app)/quick-links/+page.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<script lang="ts">
22
import type { PageData } from './$types';
33
export let data: PageData;
4-
import { page } from '$app/stores';
54
import toast from 'svelte-french-toast';
65
import { superForm } from 'sveltekit-superforms/client';
76
import Icon from '$lib/components/Icon.svelte';
@@ -94,10 +93,10 @@
9493
</div>
9594
</div>
9695
<Badge status={link.status} />
97-
<a target="_blank" href="/pay?id={link.id}">
96+
<a target="_blank" href="https://pay.invoicelink.io/{link.id}">
9897
<Icon name="launch" />
9998
</a>
100-
<CopyToClipboard text={`${$page.url.origin}/pay?id=${link.id}`} />
99+
<CopyToClipboard text={`https://pay.invoicelink.io/${link.id}`} />
101100
</li>
102101
{/each}
103102
</ul>

0 commit comments

Comments
 (0)