-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Johannes Groß <mail@gross-johannes.de>
- Loading branch information
Showing
5 changed files
with
139 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
pages/api/workspaces/[workspaceId]/cards/[cardId]/clone.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { withHttpMethods } from '../../../../../../middleware/api/handleMethods'; | ||
import HTTPMethod from 'http-method-enum'; | ||
import { withWorkspacePermission } from '../../../../../../middleware/api/authenticationMiddleware'; | ||
import { Role } from '@prisma/client'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import prisma from '../../../../../../lib/prisma'; | ||
|
||
export default withHttpMethods({ | ||
[HTTPMethod.POST]: withWorkspacePermission([Role.MANAGER], async (req: NextApiRequest, res: NextApiResponse, user, workspace) => { | ||
const cardId = req.query.cardId as string | undefined; | ||
if (!cardId) return res.status(400).json({ message: 'No card id' }); | ||
const { name } = JSON.parse(req.body); | ||
|
||
return prisma.$transaction(async (transaction) => { | ||
const existing = await prisma.cocktailCard.findFirst({ | ||
where: { | ||
id: cardId, | ||
workspaceId: workspace.id, | ||
}, | ||
}); | ||
if (!existing) return res.status(404).json({ message: 'Card not found' }); | ||
|
||
const createClone = await transaction.cocktailCard.create({ | ||
data: { | ||
id: undefined, | ||
name: name, | ||
createdAt: undefined, | ||
updatedAt: undefined, | ||
workspaceId: workspace.id, | ||
showTime: existing.showTime, | ||
date: existing.date, | ||
archived: existing.archived, | ||
}, | ||
}); | ||
|
||
const groups = await prisma.cocktailCardGroup.findMany({ | ||
where: { | ||
cocktailCardId: existing.id, | ||
}, | ||
include: { | ||
items: true, | ||
}, | ||
}); | ||
|
||
for (const group of groups) { | ||
const createGroup = await transaction.cocktailCardGroup.create({ | ||
data: { | ||
id: undefined, | ||
name: group.name, | ||
groupNumber: group.groupNumber, | ||
groupPrice: group.groupPrice, | ||
items: {}, | ||
cocktailCard: { connect: { id: createClone.id } }, | ||
}, | ||
}); | ||
|
||
for (const item of group.items) { | ||
await transaction.cocktailCardGroupItem.create({ | ||
data: { | ||
itemNumber: item.itemNumber, | ||
specialPrice: item.specialPrice, | ||
cocktailCardGroup: { connect: { id: createGroup.id } }, | ||
cocktail: { connect: { id: item.cocktailId } }, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
return res.json({ data: createClone }); | ||
}); | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters