Generador de orders completado sin fulfillments

This commit is contained in:
2026-01-05 10:57:53 +01:00
parent fdd1de37d7
commit 449be9d950
5 changed files with 149 additions and 14 deletions

View File

@@ -8,24 +8,23 @@ import { ADDRESS } from "#data/data-pools/ADDRESS.js";
import { CUSTOMER } from "#data/data-pools/CUSTOMER.js";
import { FieldPool, PoolOf } from "#data/data-pools/pool.type.js";
import { Shopify } from "../webhooks/order";
import { LINE_ITEM } from "./LINE_ITEM";
import { PRICE_SET, randomPriceSet } from "./PRICE_SET";
import { CURRENCIES, pickCurrencies } from "./shared";
import { CURRENCIES, pickCurrencies, randomDecimal, randomDecimalGenerator, randomEmail, randomInteger, randomIntegerGenerator, randomISODate } from "./shared";
import { TAX_LINES } from "./TAX_LINES";
function genId(digits: number) {
return Math.floor(Math.random() * (10 ^ digits))
}
function generatePrice(enteros: number, decimales: number) {
return (Math.floor(
Math.random() * (10 ** (enteros + decimales + 1)))
/
(10 ** (decimales))
).toString()
function randomID() {
return Math.random().toString(36).substring(3)
}
export function generateOrder(pool: PoolOf<Shopify.Order>) {
const id = genId(8)
const price = generatePrice(2, 2)
const price = randomDecimal(2, 2)
const shopCurrency = pickCurrencies(CURRENCIES)
const ORDER = <Shopify.Order>{
@@ -52,8 +51,55 @@ export const ORDER = new FieldPool<Shopify.Order>({
customer: CUSTOMER.randomValue,
shipping_address: ADDRESS.randomValue,
currency: () => pickCurrencies(CURRENCIES),
current_subtotal_price: () => generatePrice(2, 2),
current_subtotal_price: () => randomDecimal(2, 2),
current_subtotal_price_set: PRICE_SET.randomValue,
current_total_discounts: randomDecimalGenerator(3, 2),
current_total_discounts_set: PRICE_SET.randomValue,
current_total_duties_set: PRICE_SET.randomValue,
current_total_tax: randomDecimalGenerator(1, 2),
current_total_tax_set: PRICE_SET.randomValue,
customer_locale: ["es-ES", "pt-PT", "fr-FR", "en-GB"],
device_id: randomDecimalGenerator(10, 0),
discount_codes: [],
duties_included: [true, false],
email: randomEmail,
estimated_taxes: [true, false],
financial_status: ["paid", "unpaid", "pending"],
fulfillment_status: ["CANCELLED", "ERROR", "FAILURE", "SUCCESS", null],
landing_site: ["/"],
landing_site_ref: [""],
location_id: randomDecimalGenerator(10, 0),
merchant_business_entity_id: randomID,
merchant_of_record_app_id: randomID,
name: randomID,
note: [""],
note_attributes: [
// No se como se genera ni que hacer con ello
],
tax_exempt: [true, false],
taxes_included: [true, false],
tax_lines: [[TAX_LINES.randomValue()]], //! No he tenido en cuenta que hay que generar una lista de items
token: ["token1", "token2"],
total_cash_rounding_payment_adjustment_set: PRICE_SET.randomValue,
total_cash_rounding_refund_adjustment_set: PRICE_SET.randomValue,
total_discounts: randomDecimalGenerator(1, 2),
total_discounts_set: PRICE_SET.randomValue,
total_line_items_price: randomDecimalGenerator(3, 2),
total_line_items_price_set: PRICE_SET.randomValue,
total_price: randomDecimalGenerator(3, 2),
total_price_set: PRICE_SET.randomValue,
total_shipping_price_set: PRICE_SET.randomValue,
total_tax: randomDecimalGenerator(1, 2),
total_tax_set: PRICE_SET.randomValue,
total_tip_received: randomDecimalGenerator(2, 2),
total_weight: randomIntegerGenerator(100, 0),
updated_at: randomISODate,
user_id: randomID,
billing_address: ADDRESS.randomValue,
fulfillments: [],
line_items: () => LINE_ITEM.randomList(3),
}, generateOrder)
export const INVALID_POOL = {

View File

@@ -0,0 +1,13 @@
import { Shopify } from "#data/webhooks/order.js";
import { FieldPool, PoolOf } from "./pool.type";
import { PRICE_SET } from "./PRICE_SET";
import { randomDecimalGenerator } from "./shared";
export const TAX_LINES = new FieldPool(<PoolOf<Shopify.TaxLine>>{
price: randomDecimalGenerator(3, 2),
rate: [0.16, 0.21],
title: ["VAT", "IVA"],
price_set: PRICE_SET.randomValue,
channel_liable: [true, false]
})

View File

@@ -1,4 +1,5 @@
import { NullablePartial } from "#shared/NullablePartail.js";
import { randomInteger } from "./shared";
/**
* Una pool de valores derivada de un tipo existente
@@ -43,7 +44,10 @@ function getRandomOfField<T>(field: PoolField<T> | undefined) {
/**
* Un FieldPool es un wraper de PoolOf que asocia una funcion
* aleatoria personalizada al tipo de la pool
* aleatoria personalizada al tipo de la pool.
* Por defecto los valores de los campos se generan de forma aleatoria
* a no ser que se use la funcion personalizada que puede "fijar" ciertos
* campos
*/
export class FieldPool<T> {
public pool: PoolOf<T>
@@ -61,6 +65,7 @@ export class FieldPool<T> {
}
}
public static pickRandomOfField = getRandomOfField
public static pickRandomOf = getRandomOf
@@ -85,4 +90,16 @@ export class FieldPool<T> {
return mock
}
public randomList(length: number) {
const numberEntries = length ?? randomInteger(10, 1)
const list = []
for (let i = 0; i < numberEntries; i++) {
const value = this.randomValue()
list.push(value)
}
return list
}
}

View File

@@ -12,3 +12,62 @@ export function pickCurrencies(currencies: string[]) {
const select = Math.floor(Math.random() * (max - min)) + min
return currencies[select]
}
export function randomDecimal(enteros: number, decimales: number) {
return (Math.floor(
Math.random() * (10 ** (enteros + decimales + 1)))
/
(10 ** (decimales))
).toString()
}
export function randomDecimalGenerator(enteros: number, decimales: number) {
return () => randomDecimal(enteros, decimales)
}
export function randomEmail() {
const domains = ["gmail.com", "outlook.com"]
const seleccionDominio = Math.floor(Math.random() * domains.length)
const randName = Math.random().toString(36).substring(3)
return randName + "@" + domains[seleccionDominio]
}
export function randomIntegerGenerator(max: number, min: number) {
return () => randomInteger(max, min)
}
export function randomInteger(max: number, min: number) {
return Math.floor(Math.random() * (max + min + 1) - min)
}
export function randomISODate() {
// 1. Generate a random date (between year 2000 and now)
const start = new Date(2000, 0, 1).getTime();
const end = new Date().getTime();
const date = new Date(Math.floor(Math.random() * (end - start + 1) + start));
// 2. Generate a random offset
// Standard offsets range from -12 to +14 hours.
// We'll pick a random hour and either 00, 30, or 45 minutes.
const offsetHours = Math.floor(Math.random() * (14 - (-12) + 1)) + (-12);
const offsetMinutes = [0, 30, 45][Math.floor(Math.random() * 3)];
// 3. Format the offset string (e.g., +05:30 or -08:00)
const sign = offsetHours >= 0 ? "+" : "-";
const absHours = Math.abs(offsetHours).toString().padStart(2, '0');
const absMinutes = offsetMinutes.toString().padStart(2, '0');
const offsetStr = `${sign}${absHours}:${absMinutes}`;
// 4. Manually construct the ISO string
const pad = (n: number) => n.toString().padStart(2, '0');
const year = date.getFullYear();
const month = pad(date.getMonth() + 1);
const day = pad(date.getDate());
const hours = pad(date.getHours());
const minutes = pad(date.getMinutes());
const seconds = pad(date.getSeconds());
const ms = date.getMilliseconds().toString().padStart(3, '0');
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${ms}${offsetStr}`;
}

View File

@@ -125,13 +125,13 @@ export declare namespace Shopify {
province_code: string,
}
export type TaxLine = {
export type TaxLine = NullablePartial<{
price: string,
rate: 0.21,
title: string,
price_set: PriceSet,
channel_liable: boolean
}
}>
export type NoteAtributte = {
name: string,
@@ -233,8 +233,8 @@ export declare namespace Shopify {
tags: string,
tax_exempt: boolean,
tax_lines: TaxLine[],
taxes_included: true,
test: false,
taxes_included: boolean,
test: boolean,
token: string,
total_cash_rounding_payment_adjustment_set: PriceSet,
total_cash_rounding_refund_adjustment_set: PriceSet,