Skip to content

Commit

Permalink
🚧 customers crud - add status field, enable seed and query routes
Browse files Browse the repository at this point in the history
  • Loading branch information
MammaSonnim committed Jan 20, 2025
1 parent 9737805 commit f4df377
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/lib/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Customer {
name: string;
email: string;
image_url: string;
status: 'active' | 'inactive' | 'deleted';
}

export interface Invoice {
Expand Down
13 changes: 13 additions & 0 deletions app/lib/placeholder-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,49 @@ const customers = [
name: 'Evil Rabbit',
email: '[email protected]',
image_url: '/customers/evil-rabbit.png',
status: 'active',
},
{
id: '3958dc9e-712f-4377-85e9-fec4b6a6442a',
name: 'Delba de Oliveira',
email: '[email protected]',
image_url: '/customers/delba-de-oliveira.png',
status: 'active',
},
{
id: '3958dc9e-742f-4377-85e9-fec4b6a6442a',
name: 'Lee Robinson',
email: '[email protected]',
image_url: '/customers/lee-robinson.png',
status: 'active',
},
{
id: '76d65c26-f784-44a2-ac19-586678f7c2f2',
name: 'Michael Novotny',
email: '[email protected]',
image_url: '/customers/michael-novotny.png',
status: 'active',
},
{
id: 'CC27C14A-0ACF-4F4A-A6C9-D45682C144B9',
name: 'Amy Burns',
email: '[email protected]',
image_url: '/customers/amy-burns.png',
status: 'active',
},
{
id: '13D07535-C59E-4157-A011-F8D2EF4E0CBB',
name: 'Balazs Orban',
email: '[email protected]',
image_url: '/customers/balazs-orban.png',
status: 'active',
},
{
id: '15D07535-C79E-4157-A031-F8D2EF4E0CBB',
name: 'Kind Rabbit',
email: '[email protected]',
image_url: '/customers/kind-rabbit.png',
status: 'deleted',
},
];

Expand Down
26 changes: 26 additions & 0 deletions app/query/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { db } from '@vercel/postgres';

const client = await db.connect();

// TODO edit somehow data, drop or migration
async function listInvoices() {
// const data = await client.sql`
// SELECT invoices.amount, customers.name
// FROM invoices
// JOIN customers ON invoices.customer_id = customers.id
// WHERE invoices.amount = 666;
// `;
const data = await client.sql`
DROP table customers;
`;

return data;
}

export async function GET() {
try {
Response.json(await listInvoices());
} catch (error) {
return Response.json({ error }, { status: 500 });
}
}
7 changes: 4 additions & 3 deletions app/seed/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ async function seedCustomers() {
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
image_url VARCHAR(255) NOT NULL
image_url VARCHAR(255) NOT NULL,
status VARCHAR(255) NOT NULL
);
`;

const insertedCustomers = await Promise.all(
customers.map(
(customer) => client.sql`
INSERT INTO customers (id, name, email, image_url)
VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url})
INSERT INTO customers (id, name, email, image_url, status)
VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url}, ${customer.status})
ON CONFLICT (id) DO NOTHING;
`,
),
Expand Down
10 changes: 3 additions & 7 deletions auth.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ export const authConfig = {
callbacks: {
authorized({ auth, request: { nextUrl } }) {
const isLoggedIn = Boolean(auth?.user);
const isOnDashboard = nextUrl.pathname.startsWith('/dashboard');
const isOnLoginPage = nextUrl.pathname === '/login';

if (isOnDashboard) {
return isLoggedIn;
}

if (isLoggedIn) {
if (isOnLoginPage && isLoggedIn) {
return Response.redirect(new URL('/dashboard', nextUrl));
}

return true;
return isLoggedIn || nextUrl.pathname === '/login';
},
},
providers: [], // Add providers with an empty array for now
Expand Down
Binary file added public/customers/kind-rabbit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f4df377

Please sign in to comment.