This repository has been archived on 2026-01-31. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
nextjs-serve-actions/app/components/Dashboard/Items/actions/ItemsTableAction.ts
2024-05-23 16:55:29 +02:00

49 lines
851 B
TypeScript

'use server';
import { Items } from '../../../../../data/types';
import prisma from '../../../../../prisma/prisma';
interface ItemsTableActionProps {
take: number;
profile: string | undefined;
}
export async function ItemsTableAction({
take,
profile
}: ItemsTableActionProps) {
if (!profile) {
return [] as Items;
}
try {
const items = await prisma.item.findMany({
where: {
profileId: profile
},
orderBy: {
createdAt: 'desc'
},
include: {
Tag: {
select: {
name: true
}
}
},
take
});
const itemsWithTag = items.map((item) => {
return {
...item,
tag: item?.Tag?.name
};
});
return itemsWithTag as Items;
} catch (error) {
throw new Error('Failed to find items');
}
}