176 lines
5 KiB
TypeScript
176 lines
5 KiB
TypeScript
import { NextResponse, NextRequest } from 'next/server'
|
|
import { validateToken, decryptToken } from '@/lib/jwt'
|
|
import { db } from "@/lib/db"
|
|
import { logging } from '@/lib/logging'
|
|
|
|
interface orCardFilterProps {
|
|
sanitized_name: string
|
|
OR: orSetFilterProps[]
|
|
}
|
|
|
|
interface orSetFilterProps {
|
|
set_code: string
|
|
}
|
|
|
|
interface cardEntryAPIProps {
|
|
amount: number,
|
|
sanitized_name: string,
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const token = req?.headers.get("authorization")?.split(" ")[1]
|
|
const { name, cards, url, selected_bset, commander_name } = await req.json()
|
|
|
|
console.log(url)
|
|
|
|
if(token == undefined) {
|
|
return NextResponse.json({"message": "You did not provide a token."},{
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
if(!validateToken(token)) {
|
|
return NextResponse.json({"message": "Your token is not valid."},{
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
const tokenData = decryptToken(token)
|
|
|
|
if(name == undefined || cards == undefined || selected_bset == undefined || commander_name == undefined) {
|
|
return NextResponse.json({"message": "Wrong data in the request."},{
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
const bset = await db.bset.findFirst({
|
|
where: {
|
|
sanitized_name: selected_bset
|
|
},
|
|
relationLoadStrategy: "join",
|
|
include: {
|
|
sets: true
|
|
}
|
|
})
|
|
|
|
const set_codes: orSetFilterProps[] = []
|
|
bset?.sets.forEach((set) => {
|
|
set_codes.push({set_code: set.code})
|
|
})
|
|
|
|
const cardsFilter: orCardFilterProps[] = [{sanitized_name: commander_name, OR: set_codes}]
|
|
cards.forEach((card:cardEntryAPIProps) => {
|
|
cardsFilter.push({sanitized_name: card.sanitized_name, OR: set_codes})
|
|
})
|
|
|
|
let cardsData = await db.carte.findMany({
|
|
where: {
|
|
OR: set_codes
|
|
}
|
|
})
|
|
|
|
// Sort cards to select non promo types first and fallback to promo cards if not found
|
|
cardsData = cardsData.sort((a,b) => +a.is_promo - +b.is_promo)
|
|
|
|
const cardsNotFound = []
|
|
|
|
let allCardFound = true
|
|
if(cardsData.findIndex(cardData => cardData.sanitized_name == commander_name) == -1){
|
|
if(cardsData.findIndex(cardData => cardData.sanitized_name.includes(commander_name)) == -1){
|
|
console.log("Not Found : " + commander_name)
|
|
allCardFound = false
|
|
cardsNotFound.push(commander_name)
|
|
}
|
|
}
|
|
cards.forEach((card: cardEntryAPIProps) => {
|
|
if(cardsData.findIndex(cardData => cardData.sanitized_name == card.sanitized_name) == -1){
|
|
if(cardsData.findIndex(cardData => cardData.sanitized_name.includes(card.sanitized_name)) == -1 ){
|
|
allCardFound = false
|
|
console.log("Not Found : Bset = " + selected_bset + " , name = " + card.sanitized_name)
|
|
cardsNotFound.push(card.sanitized_name)
|
|
}
|
|
}
|
|
})
|
|
|
|
if(!allCardFound) {
|
|
return NextResponse.json({"message": "Some cards were not found... " + cardsNotFound.join(", ")},{
|
|
status: 401,
|
|
});
|
|
}
|
|
|
|
const commander_card = cardsData.findIndex(cardData => cardData.sanitized_name == commander_name) == -1 ? cardsData[cardsData.findIndex(cardData => cardData.sanitized_name.includes(commander_name))] : cardsData[cardsData.findIndex(cardData => cardData.sanitized_name == commander_name)]
|
|
|
|
const deck = await db.deck.create({
|
|
data: {
|
|
name,
|
|
url,
|
|
color_identity: commander_card.color_identity,
|
|
utilisateurice: {
|
|
connect: {
|
|
id: tokenData.id
|
|
}
|
|
},
|
|
commander: {
|
|
connect: {
|
|
id: commander_card.id
|
|
}
|
|
},
|
|
bset: {
|
|
connect: {
|
|
id: bset?.id
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
cards.forEach(async (card: cardEntryAPIProps) => {
|
|
const cardData_id = cardsData.findIndex(cardData => cardData.sanitized_name == card.sanitized_name) == -1 ? cardsData[cardsData.findIndex(cardData => cardData.sanitized_name.includes(card.sanitized_name))].id : cardsData[cardsData.findIndex(cardData => cardData.sanitized_name == card.sanitized_name)].id
|
|
await db.cartes_dans_deck.create({
|
|
data: {
|
|
amount: card.amount,
|
|
carte: {
|
|
connect: {
|
|
id: cardData_id
|
|
}
|
|
},
|
|
deck: {
|
|
connect: {
|
|
id: deck.id
|
|
}
|
|
}
|
|
}
|
|
})
|
|
})
|
|
|
|
// Necessary to fetch complete deck with cards to display it to the user
|
|
const deck_complete = await db.deck.findFirst({
|
|
where: {
|
|
id: deck.id
|
|
},
|
|
relationLoadStrategy: "join",
|
|
include: {
|
|
cartes: {
|
|
include: {
|
|
carte: true
|
|
}
|
|
},
|
|
commander: true
|
|
}
|
|
})
|
|
|
|
logging("Deck created by " + tokenData.username + " : " + name)
|
|
|
|
return NextResponse.json({"data": deck_complete, "message": "Deck created !"},{
|
|
status: 200,
|
|
});
|
|
} catch (error) {
|
|
console.log(error)
|
|
return NextResponse.json(
|
|
{ error: "Failed, check console" },
|
|
{
|
|
status: 500,
|
|
}
|
|
);
|
|
}
|
|
}
|