Fix(WIP): CORS
This commit is contained in:
		@@ -14,7 +14,8 @@ import ApiImplementation from './services/api-implementation';
 | 
				
			|||||||
// Composables
 | 
					// Composables
 | 
				
			||||||
import { createApp } from 'vue'
 | 
					import { createApp } from 'vue'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
process.env.API_IMPLEMENTATION = 'django';
 | 
					process.env.API_IMPLEMENTATION = 'tryton';
 | 
				
			||||||
 | 
					// process.env.API_IMPLEMENTATION = 'django';
 | 
				
			||||||
let apiImplementation = new ApiImplementation();
 | 
					let apiImplementation = new ApiImplementation();
 | 
				
			||||||
const api = apiImplementation.getApi();
 | 
					const api = apiImplementation.getApi();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,7 +9,7 @@ class ApiImplementation {
 | 
				
			|||||||
    if (implementation === 'django') {
 | 
					    if (implementation === 'django') {
 | 
				
			||||||
      apiImplementation = new DjangoApi();
 | 
					      apiImplementation = new DjangoApi();
 | 
				
			||||||
    } else if (implementation === 'tryton'){
 | 
					    } else if (implementation === 'tryton'){
 | 
				
			||||||
      const url = 'http:localhost:8000';
 | 
					      const url = 'http://localhost:8000';
 | 
				
			||||||
      const key = '9a9ffc430146447d81e6698240199a4be2b0e774cb18474999d0f60e33b5b1eb1cfff9d9141346a98844879b5a9e787489c891ddc8fb45cc903b7244cab64fb1';
 | 
					      const key = '9a9ffc430146447d81e6698240199a4be2b0e774cb18474999d0f60e33b5b1eb1cfff9d9141346a98844879b5a9e787489c891ddc8fb45cc903b7244cab64fb1';
 | 
				
			||||||
      const db = 'tryton';
 | 
					      const db = 'tryton';
 | 
				
			||||||
      const applicationName = 'sale_don_confiao';
 | 
					      const applicationName = 'sale_don_confiao';
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,82 +3,107 @@ class TrytonApiClient {
 | 
				
			|||||||
    this.baseUrl = `${url}/${db}/${applicationName}`;
 | 
					    this.baseUrl = `${url}/${db}/${applicationName}`;
 | 
				
			||||||
    this.headers = {
 | 
					    this.headers = {
 | 
				
			||||||
      'Authorization': `Bearer ${key}`,
 | 
					      'Authorization': `Bearer ${key}`,
 | 
				
			||||||
      'Content-Type': 'application/json'
 | 
					      'Content-Type': 'application/json',
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async getParties() {
 | 
					  getCustomers() {
 | 
				
			||||||
    return this._fetch('/parties');
 | 
					    // const url = '/don_confiao/api/customers/';
 | 
				
			||||||
 | 
					    const url = this.baseUrl + '/parties';
 | 
				
			||||||
 | 
					    const customers = this.getRequest(url);
 | 
				
			||||||
 | 
					    return customers;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async getParty(id){
 | 
					  getProducts() {
 | 
				
			||||||
    return this._fetch(`/party/${id}`);
 | 
					    const url = '/don_confiao/api/products/';
 | 
				
			||||||
 | 
					    return this.getRequest(url);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async postParty(name) {
 | 
					  getPaymentMethods() {
 | 
				
			||||||
    return this._post('/parties', { name });
 | 
					    const url = '/don_confiao/payment_methods/all/select_format';
 | 
				
			||||||
 | 
					    return this.getRequest(url);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async getCategories() {
 | 
					  getSummaryPurchase(purchaseId) {
 | 
				
			||||||
    return this._fetch('/categories');
 | 
					    const url = `/don_confiao/resumen_compra_json/${purchaseId}`;
 | 
				
			||||||
 | 
					    return this.getRequest(url);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async getProducts() {
 | 
					  getPurchasesForReconciliation() {
 | 
				
			||||||
    return this._fetch('/products');
 | 
					    const url = '/don_confiao/purchases/for_reconciliation';
 | 
				
			||||||
 | 
					    return this.getRequest(url);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async searchProducts(query) {
 | 
					  isValidAdminCode(code) {
 | 
				
			||||||
    return this._fetch(`/search_products/${query}`);
 | 
					    const url = `/don_confiao/api/admin_code/validate/${code}`
 | 
				
			||||||
 | 
					    return this.getRequest(url)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  createPurchase(purchase) {
 | 
				
			||||||
 | 
					    const url = '/don_confiao/api/sales/';
 | 
				
			||||||
 | 
					    return this.postRequest(url, purchase);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async getSales() {
 | 
					  createReconciliationJar(reconciliation) {
 | 
				
			||||||
    return this._fetch('/sales');
 | 
					    const url = '/don_confiao/reconciliate_jar';
 | 
				
			||||||
 | 
					    return this.postRequest(url, reconciliation);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async getSale(id) {
 | 
					  createCustomer(customer) {
 | 
				
			||||||
    return this._fetch(`/sale/${id}`);
 | 
					    const url = '/don_confiao/api/customers/';
 | 
				
			||||||
 | 
					    return this.postRequest(url, customer);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async postSale(saleData) {
 | 
					  getRequest(url) {
 | 
				
			||||||
    return this._post('/post_sale', saleData);
 | 
					    return new Promise ((resolve, reject) => {
 | 
				
			||||||
  }
 | 
					      fetch(url, {
 | 
				
			||||||
 | 
					        method: 'GET',
 | 
				
			||||||
  async _fetch(endpoint) {
 | 
					        headers: this.headers
 | 
				
			||||||
    const response = await fetch(`${this.baseUrl}${endpoint}`, {
 | 
					      }).then(response => response.json())
 | 
				
			||||||
      method: 'GET',
 | 
					        .then(data => {
 | 
				
			||||||
      headers: this.headers
 | 
					          resolve(data);
 | 
				
			||||||
    });
 | 
					        })
 | 
				
			||||||
 | 
					        .catch(error => {
 | 
				
			||||||
    return this._handleResponse(response);
 | 
					          reject(error);
 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      async _post(endpoint, data) {
 | 
					 | 
				
			||||||
        const response = await fetch(`${this.baseUrl}${endpoint}`, {
 | 
					 | 
				
			||||||
            method: 'POST',
 | 
					 | 
				
			||||||
            headers: this.headers,
 | 
					 | 
				
			||||||
            body: JSON.stringify(data)
 | 
					 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
        return this._handleResponse(response);
 | 
					    });
 | 
				
			||||||
    }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    async _handleResponse(response) {
 | 
					  postRequest(url, content) {
 | 
				
			||||||
        if (!response.ok) {
 | 
					    return new Promise((resolve, reject) => {
 | 
				
			||||||
            throw new Error(`HTTP error! status: ${response.status}`);
 | 
					      fetch(url, {
 | 
				
			||||||
        }
 | 
					        method: 'POST',
 | 
				
			||||||
        return response.json();
 | 
					        headers: this.headers,
 | 
				
			||||||
    }
 | 
					        body: JSON.stringify(content)
 | 
				
			||||||
 | 
					      })
 | 
				
			||||||
 | 
					        .then(response => {
 | 
				
			||||||
 | 
					          if (!response.ok) {
 | 
				
			||||||
 | 
					            reject(new Error(`Error ${response.status}: ${response.statusText}`));
 | 
				
			||||||
 | 
					          } else {
 | 
				
			||||||
 | 
					            response.json().then(data => {
 | 
				
			||||||
 | 
					              if (!data) {
 | 
				
			||||||
 | 
					                reject(new Error('La respuesta no es un JSON válido'));
 | 
				
			||||||
 | 
					              } else {
 | 
				
			||||||
 | 
					                resolve(data);
 | 
				
			||||||
 | 
					              }
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					        .catch(error => reject(error));
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default TrytonApiClient;
 | 
					// export default TrytonApiClient;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const url = 'http:localhost:8000';
 | 
					// const url = 'http://localhost:8000';
 | 
				
			||||||
const key = '9a9ffc430146447d81e6698240199a4be2b0e774cb18474999d0f60e33b5b1eb1cfff9d9141346a98844879b5a9e787489c891ddc8fb45cc903b7244cab64fb1';
 | 
					// const key = '9a9ffc430146447d81e6698240199a4be2b0e774cb18474999d0f60e33b5b1eb1cfff9d9141346a98844879b5a9e787489c891ddc8fb45cc903b7244cab64fb1';
 | 
				
			||||||
const db = 'tryton';
 | 
					// const db = 'tryton';
 | 
				
			||||||
const applicationName = 'sale_don_confiao';
 | 
					// const applicationName = 'sale_don_confiao';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const apiClient = new TrytonApiClient(url, key, db, applicationName);
 | 
					// const apiClient = new TrytonApiClient(url, key, db, applicationName);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
console.log(
 | 
					// console.log(
 | 
				
			||||||
  apiClient.getProducts()
 | 
					//   apiClient.getCustomers()
 | 
				
			||||||
           .then(data => console.log(data))
 | 
					//            .then(data => console.log(data))
 | 
				
			||||||
           .catch(error => console.error('Error:', error)))
 | 
					//            .catch(error => console.error('Error:', error)))
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,6 +11,8 @@ import Vuetify, { transformAssetUrls } from 'vite-plugin-vuetify'
 | 
				
			|||||||
import { defineConfig } from 'vite'
 | 
					import { defineConfig } from 'vite'
 | 
				
			||||||
import { fileURLToPath, URL } from 'node:url'
 | 
					import { fileURLToPath, URL } from 'node:url'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// https://vitejs.dev/config/
 | 
					// https://vitejs.dev/config/
 | 
				
			||||||
export default defineConfig({
 | 
					export default defineConfig({
 | 
				
			||||||
  plugins: [
 | 
					  plugins: [
 | 
				
			||||||
@@ -63,6 +65,13 @@ export default defineConfig({
 | 
				
			|||||||
  },
 | 
					  },
 | 
				
			||||||
  server: {
 | 
					  server: {
 | 
				
			||||||
    port: 3000,
 | 
					    port: 3000,
 | 
				
			||||||
 | 
					    proxy: {
 | 
				
			||||||
 | 
					      '/sale_don_confiao': {
 | 
				
			||||||
 | 
					        target: "http://localhost:8000", // Cambia esto a la URL de tu API
 | 
				
			||||||
 | 
					        changeOrigin: true,
 | 
				
			||||||
 | 
					        rewrite: (path) => path.replace(/^\/sale_don_confiao/, '/tryton/sale_don_confiao'), // Opcional: reescribe la ruta
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  build: {
 | 
					  build: {
 | 
				
			||||||
    outDir: '../../static/frontend/', 
 | 
					    outDir: '../../static/frontend/', 
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,6 +28,10 @@ DEBUG = True
 | 
				
			|||||||
ALLOWED_HOSTS = []
 | 
					ALLOWED_HOSTS = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CORS_ALLOWED_ORIGINS = [
 | 
				
			||||||
 | 
					    "http://localhost:8000",
 | 
				
			||||||
 | 
					]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Application definition
 | 
					# Application definition
 | 
				
			||||||
 | 
					
 | 
				
			||||||
INSTALLED_APPS = [
 | 
					INSTALLED_APPS = [
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user