diff --git a/src/app/api/image/estimate-image-copy/route.js b/src/app/api/image/estimate-image-copy/route.js new file mode 100644 index 00000000..720dd5f0 --- /dev/null +++ b/src/app/api/image/estimate-image-copy/route.js @@ -0,0 +1,42 @@ +import { NextResponse } from 'next/server' +import { S3Client, CopyObjectCommand } from '@aws-sdk/client-s3' +import sharp from 'sharp' +import { v4 as uuidv4 } from 'uuid' +const Bucket = process.env.AMPLIFY_BUCKET +const s3 = new S3Client({ + region: process.env.AWS_REGION, + credentials: { + accessKeyId: process.env.AWS_ACCESS_KEY_ID, + secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, + }, +}) + +export async function POST(req) { + try { + const { objectNo, planNo, newObjectNo, newPlanNo } = await req.json() + + const responseArray = [] + + const copyCommand = new CopyObjectCommand({ + Bucket, + CopySource: encodeURI(`${Bucket}/Drawing/${objectNo}_${planNo}_1.png`), + Key: `Drawing/${newObjectNo}_${newPlanNo}_1.png`, + }) + + const response = await s3.send(copyCommand) + + const copyCommand2 = new CopyObjectCommand({ + Bucket, + CopySource: encodeURI(`${Bucket}/Drawing/${objectNo}_${planNo}_2.png`), + Key: `Drawing/${newObjectNo}_${newPlanNo}_2.png`, + }) + + const response2 = await s3.send(copyCommand2) + + responseArray.push(response, response2) + return NextResponse.json({ message: '견적서 이미지 복사 성공', responseArray }, { status: 200 }) + } catch (error) { + console.error(error) + return NextResponse.json({ message: '견적서 이미지 복사 실패' }, { status: 500 }) + } +} diff --git a/src/hooks/floorPlan/estimate/useEstimateController.js b/src/hooks/floorPlan/estimate/useEstimateController.js index ae37c8db..64ff13e9 100644 --- a/src/hooks/floorPlan/estimate/useEstimateController.js +++ b/src/hooks/floorPlan/estimate/useEstimateController.js @@ -13,6 +13,8 @@ import { useSwal } from '@/hooks/useSwal' // Constants const ESTIMATE_API_ENDPOINT = '/api/estimate' // API 엔드포인트 정의 +import Config from '@/config/config.export' + // Helper functions const updateItemInList = (itemList, dispOrder, updates) => { return itemList.map((item) => (item.dispOrder === dispOrder ? { ...item, ...updates } : item)) @@ -464,11 +466,13 @@ export const useEstimateController = (planNo, flag) => { setIsGlobalLoading(true) await promisePost({ url: '/api/estimate/save-estimate-copy', data: params }) - .then((res) => { + .then(async (res) => { setIsGlobalLoading(false) if (res.status === 201) { if (isObjectNotEmpty(res.data)) { let newObjectNo = res.data.objectNo + const copyImage = await handleEstimateImageCopy(params.objectNo, params.planNo, newObjectNo, '1') + swalFire({ text: getMessage('estimate.detail.estimateCopyPopup.copy.alertMessage'), type: 'alert', @@ -489,6 +493,14 @@ export const useEstimateController = (planNo, flag) => { }) } + const handleEstimateImageCopy = async (objectNo, planNo, newObjectNo, newPlanNo) => { + await promisePost({ url: `${Config().baseUrl}/api/image/estimate-image-copy`, data: { objectNo, planNo, newObjectNo, newPlanNo } }).then( + (res) => { + return res + }, + ) + } + const handleDeleteEstimate = async (canvasStatus) => { try { setIsGlobalLoading(true)