Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 1x 1x 1x 1x 1x 1x 1x 1x | const { models, sequelize } = require("../config/sequelize");
const { ApiError } = require("../utils/errorHandler");
// Get all addresses for a user
const getAllAddresses = async (userId) => {
return await models.Addresses.findAll({ where: { user_id: userId } });
};
// Add a new address
const addAddress = async (userId, addressData) => {
const existingAddresses = await models.Addresses.count({
where: {
user_id: userId,
},
});
const isDefault = existingAddresses === 0; // Set is_default to true if it's the first record
return await models.Addresses.create({
user_id: userId,
...addressData,
is_verified: false,
is_default: isDefault,
});
};
// Get a specific address
const getAddressById = async (userId, addressId) => {
return await models.Addresses.findOne({
where: {
id: addressId,
user_id: userId,
},
});
};
// Verify OTP for address
const verifyAddress = async (userId, addressId, otp) => {
// TODO: Replace this with actual OTP verification logic dependancy pcm api intigration
const isOtpValid = otp === "123456";
if (!isOtpValid) {
throw new ApiError("Invalid OTP");
}
const address = await getAddressById(userId, addressId);
if (!address) {
throw new ApiError("Address not found");
}
address.is_verified = true;
address.verified_at = new Date();
await address.save();
return address;
};
// Delete address
const deleteAddress = async (userId, addressId) => {
const address = await getAddressById(userId, addressId);
if (!address) {
throw new ApiError("Address not found");
}
await address.destroy();
return true;
};
module.exports = {
getAllAddresses,
addAddress,
getAddressById,
verifyAddress,
deleteAddress,
};
|