const Shopify = require('shopify-api-node');
const { indexRepository} = require("../repositories/index.repository");
const { GoogleLoginServices } = require("./googleLogin.Services")
class ShopifyServices {
static async getClients(access_token, shopName,domain) {
console.log("soy el email desde getclientsServices: ",GoogleLoginServices.email)
const { shopifyRepository, clientRepository} = indexRepository();
const clientIdBD = await clientRepository.findClientByEmail(GoogleLoginServices.email);
console.log("clientIdBD", clientIdBD);
console.log("clientIDBD.dataValues.id", clientIdBD.dataValues.id)
console.log("-----------------------------------")
const accessTokenExists = await shopifyRepository.findShopifyCredentialsByAccessToken(access_token);
console.log("accessTokenExists", accessTokenExists)
console.log("entre al service de shopify y estas son las credenciales: ",access_token, shopName,domain)
if( accessTokenExists===null){
console.log("entre al if")
await shopifyRepository.createShopifyCredentials(
clientIdBD.dataValues.id,
shopName,
access_token,
domain);
}else{
console.log("ya existe la tienda en la base de datos")
}
const shopify = new Shopify({
shopName: shopName,
accessToken: access_token
});
const clients = await shopify.customer.list();
// console.log("clientes: ", clients)
for (const client of clients) {
const name = client.first_name;
const lastName = client.last_name;
const phone = client.phone;
console.log("cliente desde dentro del for: ", name, lastName, phone)
if (name && lastName && phone !== null) {
GoogleLoginServices.createContact(name, lastName, phone);
console.log(`Nombre: ${name} ${lastName}, Teléfono: ${phone}`);
}
}
}
}
module.exports = { ShopifyServices };