freshyo/packages/db_helper_sqlite/src/lib/const-keys.ts
2026-10-08 19:48:14 +05:30

218 lines
6.9 KiB
TypeScript

import type { ConstValueType } from '@packages/shared'
export const CONST_KEYS = {
minRegularOrderValue: 'minRegularOrderValue',
freeDeliveryThreshold: 'freeDeliveryThreshold',
deliveryCharge: 'deliveryCharge',
flashFreeDeliveryThreshold: 'flashFreeDeliveryThreshold',
flashDeliveryCharge: 'flashDeliveryCharge',
platformFeePercent: 'platformFeePercent',
taxRate: 'taxRate',
tester: 'tester',
minOrderAmountForCoupon: 'minOrderAmountForCoupon',
maxCouponDiscount: 'maxCouponDiscount',
flashDeliverySlotId: 'flashDeliverySlotId',
readableOrderId: 'readableOrderId',
versionNum: 'versionNum',
productsVersionNum: 'products_version_num',
availabilityVersionNum: 'availability_version_num',
slotsVersionNum: 'slots_version_num',
playStoreUrl: 'playStoreUrl',
appStoreUrl: 'appStoreUrl',
popularItems: 'popularItems',
allItemsOrder: 'allItemsOrder',
tagsOrder: 'tagsOrder',
isFlashDeliveryEnabled: 'isFlashDeliveryEnabled',
supportMobile: 'supportMobile',
supportEmail: 'supportEmail',
internalAppVersion: 'internalAppVersion',
welcomeMessage: 'welcomeMessage',
} as const
export const CONST_LABELS: Record<ConstKey, string> = {
minRegularOrderValue: 'Minimum Regular Order Value',
freeDeliveryThreshold: 'Free Delivery Threshold',
deliveryCharge: 'Delivery Charge',
flashFreeDeliveryThreshold: 'Flash Free Delivery Threshold',
flashDeliveryCharge: 'Flash Delivery Charge',
platformFeePercent: 'Platform Fee Percent',
taxRate: 'Tax Rate',
tester: 'Tester',
minOrderAmountForCoupon: 'Minimum Order Amount for Coupon',
maxCouponDiscount: 'Maximum Coupon Discount',
flashDeliverySlotId: 'Flash Delivery Slot ID',
readableOrderId: 'Readable Order ID',
versionNum: 'Version Number',
'products_version_num': 'Cache Version (products)',
availability_version_num: 'Availability Cache Version',
slots_version_num: 'Slots Cache Version',
playStoreUrl: 'Play Store URL',
appStoreUrl: 'App Store URL',
popularItems: 'Popular Items',
allItemsOrder: 'All Items Order',
tagsOrder: 'Tags Order',
isFlashDeliveryEnabled: 'Enable Flash Delivery',
supportMobile: 'Support Mobile',
supportEmail: 'Support Email',
internalAppVersion: 'Internal App Version',
welcomeMessage: 'Welcome Message',
}
export type ConstKey = (typeof CONST_KEYS)[keyof typeof CONST_KEYS]
export const CONST_KEYS_ARRAY = Object.values(CONST_KEYS) as ConstKey[]
export const CONST_TYPES: Record<ConstKey, ConstValueType> = {
minRegularOrderValue: 'number',
freeDeliveryThreshold: 'number',
deliveryCharge: 'number',
flashFreeDeliveryThreshold: 'number',
flashDeliveryCharge: 'number',
platformFeePercent: 'number',
taxRate: 'number',
tester: 'string',
minOrderAmountForCoupon: 'number',
maxCouponDiscount: 'number',
flashDeliverySlotId: 'number',
readableOrderId: 'number',
versionNum: 'string',
'products_version_num': 'number',
availability_version_num: 'number',
slots_version_num: 'number',
playStoreUrl: 'string',
appStoreUrl: 'string',
popularItems: 'string',
allItemsOrder: 'string',
tagsOrder: 'string',
isFlashDeliveryEnabled: 'boolean',
supportMobile: 'string',
supportEmail: 'string',
internalAppVersion: 'number',
welcomeMessage: 'string',
}
export const CONST_VISIBILITY: Record<ConstKey, boolean> = {
minRegularOrderValue: true,
freeDeliveryThreshold: true,
deliveryCharge: true,
flashFreeDeliveryThreshold: true,
flashDeliveryCharge: true,
platformFeePercent: true,
taxRate: false,
tester: false,
minOrderAmountForCoupon: true,
maxCouponDiscount: false,
flashDeliverySlotId: true,
readableOrderId: false,
versionNum: true,
'products_version_num': false,
availability_version_num: false,
slots_version_num: false,
playStoreUrl: true,
appStoreUrl: true,
popularItems: true,
allItemsOrder: true,
tagsOrder: false,
isFlashDeliveryEnabled: true,
supportMobile: true,
supportEmail: true,
internalAppVersion: true,
welcomeMessage: true,
}
/**
* Fallback values used by getAllConstants when a key has no row in
* key_val_store yet, so a newly added constant still shows up in the admin
* form (and saving it writes the real row).
*/
export const CONST_DEFAULTS: Record<ConstKey, string | number | boolean> = {
minRegularOrderValue: 300,
freeDeliveryThreshold: 300,
deliveryCharge: 20,
flashFreeDeliveryThreshold: 500,
flashDeliveryCharge: 69,
platformFeePercent: 0,
taxRate: 0,
tester: '',
minOrderAmountForCoupon: 0,
maxCouponDiscount: 0,
flashDeliverySlotId: 0,
readableOrderId: 0,
versionNum: '1.0.0',
products_version_num: 0,
availability_version_num: 0,
slots_version_num: 0,
playStoreUrl: 'https://info.freshyo.in/qr-based-download',
appStoreUrl: 'https://info.freshyo.in/qr-based-download',
popularItems: '[]',
allItemsOrder: '[]',
tagsOrder: '[]',
isFlashDeliveryEnabled: false,
supportMobile: '',
supportEmail: '',
internalAppVersion: 1,
welcomeMessage: "Hi! Thank you for your first order at Freshyo. We're glad to have you.",
}
export const castConstValue = (key: string, value: unknown) => {
if (!Object.prototype.hasOwnProperty.call(CONST_TYPES, key)) {
return value
}
const type = CONST_TYPES[key as ConstKey]
if (type === 'number') {
const parsed = Number(value)
return Number.isFinite(parsed) ? parsed : 0
}
if (type === 'boolean') {
let newVal = value+'';
if(newVal === '0') {
return false;
}
else if(newVal?.toLowerCase() === 'false') {
return false;
}
return true;
}
if (value === null || value === undefined) {
return ''
}
return String(value)
}
/** Keys stored in the global_consts table. */
export const GLOBAL_CONST_KEYS = [
'versionNum', 'playStoreUrl', 'appStoreUrl', 'internalAppVersion',
] as const
/** Keys stored in the region_consts table (one row per region). */
export const REGION_CONST_KEYS = [
'products_version_num', 'availability_version_num', 'slots_version_num',
'popularItems', 'allItemsOrder', 'tagsOrder', 'isFlashDeliveryEnabled',
'supportMobile', 'supportEmail', 'welcomeMessage',
] as const
/** Keys stored in the region_bound_consts table (one row per bound). */
export const BOUND_CONST_KEYS = [
'minRegularOrderValue', 'freeDeliveryThreshold', 'deliveryCharge',
'flashFreeDeliveryThreshold', 'flashDeliveryCharge',
] as const
/** Keys that remain in the legacy key_val_store table. */
export const LEGACY_CONST_KEYS = [
'platformFeePercent', 'taxRate', 'tester', 'minOrderAmountForCoupon',
'maxCouponDiscount', 'flashDeliverySlotId', 'readableOrderId',
] as const
export type ConstScope = 'global' | 'region' | 'bound' | 'legacy'
/** Which table each key belongs to. */
export const CONST_SCOPE: Record<string, ConstScope> = {}
for (const k of GLOBAL_CONST_KEYS) CONST_SCOPE[k] = 'global'
for (const k of REGION_CONST_KEYS) CONST_SCOPE[k] = 'region'
for (const k of BOUND_CONST_KEYS) CONST_SCOPE[k] = 'bound'
for (const k of LEGACY_CONST_KEYS) CONST_SCOPE[k] = 'legacy'