const { toSafeRedirectUrl } = require("../../utils/urlSafety") const { sanitizeDealDescriptionHtml, sanitizeOptionalPlainText, sanitizeRequiredPlainText, } = require("../../utils/inputSanitizer") function mapCreateDealRequestToDealCreateData(payload, userId) { const { title, description, url, price, originalPrice, sellerName, customSeller, couponCode, location, discountType, discountValue, } = payload const normalizedTitle = sanitizeRequiredPlainText(title, { fieldName: "TITLE", maxLength: 300 }) const normalizedDescription = sanitizeDealDescriptionHtml(description) const normalizedCouponCode = sanitizeOptionalPlainText(couponCode, { maxLength: 120 }) const normalizedLocation = sanitizeOptionalPlainText(location, { maxLength: 150 }) const normalizedSellerName = sanitizeOptionalPlainText(sellerName ?? customSeller, { maxLength: 120, }) const normalizedUrl = toSafeRedirectUrl(url) const hasUrl = Boolean(normalizedUrl) const saleType = !hasUrl ? "OFFLINE" : normalizedCouponCode ? "CODE" : "ONLINE" const hasPrice = price != null const normalizedDiscountType = hasPrice ? null : discountType ?? null const normalizedDiscountValue = hasPrice ? null : discountValue ?? null return { title: normalizedTitle, description: normalizedDescription, url: normalizedUrl, price: price ?? null, originalPrice: originalPrice ?? null, couponCode: normalizedCouponCode, location: normalizedLocation, discountType: normalizedDiscountType, discountValue: normalizedDiscountValue, saletype: saleType, // Burada customSeller yazıyoruz; servis gerektiğinde ilişkilendiriyor. customSeller: normalizedSellerName, user: { connect: { id: userId }, }, } } module.exports = { mapCreateDealRequestToDealCreateData, }