| Current Path : /var/node/inatote/Inatote-Backend/address/ |
| Current File : /var/node/inatote/Inatote-Backend/address/address.js |
import requiredParam from '../helpers/required-param.js'
import { InvalidPropertyError } from '../helpers/errors.js'
import isValidEmail from '../helpers/is-valid-email.js'
import upperFirst from '../helpers/upper-first.js'
// import makeproductList from './product-list.js'
export default function makeAddress(addressInfo = requiredParam('addressInfo')) {
const validaddress = validate(addressInfo)
const normaladdress = normalize(validaddress)
return Object.freeze(normaladdress)
function validate({
street = requiredParam('street'),
city = requiredParam('city'),
state = requiredParam('state'),
country = requiredParam('country'),
label = requiredParam('label'),
user_id = requiredParam('user_id'),
location = requiredParam('location'),
// imageURL = requiredParam('imageURL'),
...otherInfo
} = {}) {
validateLabelName(label)
validateUserId(user_id)
validatelocation(location)
// validateImageUrl(imageURL)
// validateQuantity(quantity)
return { street, city, state, country, label, user_id ,location , ...otherInfo }
function validateLabelName(label) {
if (label.length < 3) {
throw new InvalidPropertyError(
`A label must be at least 2 characters long.`
)
}
}
function validateUserId(user_id) {
if (user_id.length < 24) {
throw new InvalidPropertyError(
`A user_id must be at least 24 characters long.`
)
}
}
function validatelocation(location) {
if (location.length < 2) {
throw new InvalidPropertyError(
`Latitude and Longitude are required.`
)
}
}
// function validateImageUrl(url) {
// if (url.length < 1) {
// throw new InvalidPropertyError("Atleast one image is required")
// }
// }
// function validateUnitPrice(unitPrice) {
// if (parseFloat(unitPrice) == undefined || !parseFloat(unitPrice)) {
// throw new InvalidPropertyError(
// `Invalid price format.`
// )
// }
// }
// function validateQuantity(quantity) {
// if (parseFloat(quantity) == undefined || !parseFloat(quantity)) {
// throw new InvalidPropertyError(
// `Invalid quantity format.`
// )
// }
// }
}
function normalize({ label,is_default , ...otherInfo }) {
return {
...otherInfo,
label: upperFirst(label),
is_default : is_default ? is_default : false
}
}
}