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(client): request your latest invoice #265

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion apps/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@hookform/resolvers": "^3.9.0",
"@repo/ui": "workspace:*",
"date-fns": "^3.6.0",
"lucide-react": "^0.408.0",
"lucide-react": "^0.411.0",
"next": "^14.2.5",
"next-themes": "^0.3.0",
"react": "^18.3.1",
Expand Down
20 changes: 19 additions & 1 deletion apps/api/src/handlers/users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OpenAPIHono } from '@hono/zod-openapi';
import type { PostgrestError } from '@supabase/supabase-js';
import { sendNewsletterSubscriptionEmail } from '../libs/email.js';
import { sendInvoiceEmail, sendNewsletterSubscriptionEmail } from '../libs/email.js';
import { type User, buildHierarchy } from '../libs/hiearchy.js';
import { supAdmin, supabase } from '../libs/supabase.js';
import { zodErrorHook } from '../libs/zodError.js';
Expand All @@ -11,6 +11,7 @@ import {
getHierarchy,
getMe,
getOneUser,
getUserInvoice,
getUsersActivities,
removeUserRole,
setStatus,
Expand Down Expand Up @@ -152,6 +153,23 @@ users.openapi(subscribeNewsletter, async (c) => {
return c.json({ message: 'Subscribed to newsletter' }, 200);
});

users.openapi(getUserInvoice, async (c) => {
const user = c.get('user');
const roles = user.roles || [];
await checkRole(roles, true);
const { data, error } = await supabase.from('USERS').select('invoice').eq('id', user.id).single();

if (error || !data) {
return c.json({ error: 'Invoice not found' }, 404);
}

if (data.invoice && process.env.ENABLE_EMAILS === 'true') {
await sendInvoiceEmail(data.invoice, user.email);
}

return c.json(data, 200);
});

users.openapi(getOneUser, async (c) => {
const roles = c.get('user').roles || [];
await checkRole(roles, true);
Expand Down
26 changes: 26 additions & 0 deletions apps/api/src/libs/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,29 @@ export async function sendNewsletterSubscriptionEmail(email: string) {
`,
});
}

export async function sendInvoiceEmail(invoice: string, email: string) {
const resend = new Resend(process.env.RESEND_KEY);

if (!resend.emails) {
throw new Error('Emails feature is not enabled');
}

await resend.emails.send({
from: 'onboarding@resend.dev',
to: email,
subject: 'Votre facture Athlonix',
html: `
<html>
<body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333;">
<h2>Votre facture Athlonix</h2>
<p>Bonjour,</p>
<p>Vous trouverez à ce lien votre facture pour votre abonnement annuel à Athlonix :</p>
<p><a href="${invoice}">Voir la facture</a></p>
<p>Si vous avez des questions, n'hésitez pas à contacter notre équipe de support</p>
<p>À très bientôt !</p>
</body>
</html>
`,
});
}
25 changes: 25 additions & 0 deletions apps/api/src/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,28 @@ export const subscribeNewsletter = createRoute({
},
tags: ['user'],
});

export const getUserInvoice = createRoute({
method: 'get',
path: '/users/invoice',
summary: 'Return your last invoice',
description: 'Return your last invoice',
security: [{ Bearer: [] }],
middleware: authMiddleware,
responses: {
200: {
description: 'Successful response',
content: {
'application/json': {
schema: z.object({
invoice: z.string().nullable(),
}),
},
},
},
400: badRequestSchema,
500: serverErrorSchema,
404: notFoundSchema,
},
tags: ['user'],
});
33 changes: 28 additions & 5 deletions apps/client/app/(auth)/account/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ export default function UserAccount() {
fetchData();
}, []);

const requestInvoice = async function handleRequestInvoice() {
try {
const token = localStorage.getItem('access_token') || '';
await fetch(`${process.env.NEXT_PUBLIC_API_URL}/users/invoice`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
});
} catch (_error) {
throw new Error('Une erreur est survenue lors de la demande de facture');
} finally {
toast.success('Facture envoyée par email');
}
};

async function handleUpateUserInformation(user: User) {
try {
await updateUserInformation(user.id, user.username, user.first_name, user.last_name);
Expand Down Expand Up @@ -131,6 +148,7 @@ export default function UserAccount() {
onClick={() => {
handleUpateUserInformation(user);
}}
disabled={user?.username === '' || user?.first_name === '' || user?.last_name === ''}
>
Editer
</Button>
Expand Down Expand Up @@ -182,11 +200,16 @@ export default function UserAccount() {
</Button>
)}
{status === 'approved' && (
<Button variant="secondary" className="mr-2">
<Link href="https://billing.stripe.com/p/login/test_8wMdSB7u77k87D2bII" target="_blank">
Editer mes informations
</Link>
</Button>
<>
<Button variant="secondary" className="mr-2">
<Link href="https://billing.stripe.com/p/login/test_8wMdSB7u77k87D2bII" target="_blank">
Editer mes informations
</Link>
</Button>
<Button variant="secondary" className="mr-2" onClick={requestInvoice}>
Demander une facture
</Button>
</>
)}
</CardFooter>
</Card>
Expand Down
3 changes: 3 additions & 0 deletions apps/client/app/ui/components/AthlonixBot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ const AthlonixBot = () => {
chatButton: {
icon: 'https://static.vecteezy.com/system/resources/previews/004/996/790/non_2x/robot-chatbot-icon-sign-free-vector.jpg',
},
audio: {
disabled: true,
},
theme: {},
tooltip: { text: "Besoin d'aide ?" },
chatHistory: { storageKey: 'example_faq_bot' },
Expand Down
4 changes: 2 additions & 2 deletions apps/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
},
"dependencies": {
"@hookform/resolvers": "^3.9.0",
"@repo/ui": "workspace:*",
"@repo/types": "workspace:*",
"lucide-react": "^0.408.0",
"@repo/ui": "workspace:*",
"lucide-react": "^0.411.0",
"next": "^14.2.5",
"next-themes": "^0.3.0",
"react": "^18.3.1",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@repo/biome-config": "workspace:*",
"@repo/supabase": "workspace:*",
"@repo/typescript-config": "workspace:*",
"turbo": "^2.0.7"
"turbo": "^2.0.8"
},
"packageManager": "pnpm@9.5.0",
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"devDependencies": {
"@repo/biome-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@turbo/gen": "^2.0.7",
"@turbo/gen": "^2.0.8",
"@types/node": "^20.14.11",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
Expand Down Expand Up @@ -50,7 +50,7 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"cmdk": "^1.0.0",
"lucide-react": "^0.408.0",
"lucide-react": "^0.411.0",
"next-themes": "^0.3.0",
"react-aria": "^3.33.1",
"react-day-picker": "^8.10.1",
Expand Down
Loading
Loading