From dd0843a696b10817ebf1fa7ee57d36f37afa1d11 Mon Sep 17 00:00:00 2001 From: bry Date: Thu, 2 Nov 2023 09:39:49 -0500 Subject: [PATCH] rely on sequlize snake case --- packages/metadata-service/src/index.ts | 55 +++++++++----------------- packages/metadata-service/src/model.ts | 34 ++++++++-------- 2 files changed, 35 insertions(+), 54 deletions(-) diff --git a/packages/metadata-service/src/index.ts b/packages/metadata-service/src/index.ts index 6cbcb51c7..f13528c0e 100644 --- a/packages/metadata-service/src/index.ts +++ b/packages/metadata-service/src/index.ts @@ -77,8 +77,8 @@ server.get<{ Params: { wallet: string } }>( }); const assetJsons = keyToAssets.map((record) => { - const keyStr = decodeEntityKey(record.entityKey, { - [record.keySerialization]: {}, + const keyStr = decodeEntityKey(record.entity_key, { + [record.key_serialization]: {}, }); return generateAssetJson(record, keyStr!); }); @@ -127,7 +127,7 @@ server.get<{ Querystring: { subnetwork: string } }>( return ktas.map((kta) => { return { key_to_asset_key: kta.address, - is_active: kta.iotHotspotInfo!.isActive, + is_active: kta.iot_hotspot_info!.is_active, }; }); } else if (subnetwork === "mobile") { @@ -142,8 +142,8 @@ server.get<{ Querystring: { subnetwork: string } }>( return ktas.map((kta) => { return { key_to_asset_key: kta.address, - is_active: kta.mobileHotspotInfo!.isActive, - device_type: kta.mobileHotspotInfo!.deviceType, + is_active: kta.mobile_hotspot_info!.is_active, + device_type: kta.mobile_hotspot_info!.device_type, }; }); } @@ -159,10 +159,10 @@ function generateAssetJson(record: KeyToAsset, keyStr: string) { const hotspotType = keyStr.length > 100 ? "MOBILE" : "IOT"; const image = `${SHDW_DRIVE_URL}/${ hotspotType === "MOBILE" - ? record?.mobileHotspotInfo?.isActive + ? record?.mobile_hotspot_info?.is_active ? "mobile-hotspot-active.png" : "mobile-hotspot.png" - : record?.iotHotspotInfo?.isActive + : record?.iot_hotspot_info?.is_active ? "hotspot-active.png" : "hotspot.png" }`; @@ -176,11 +176,11 @@ function generateAssetJson(record: KeyToAsset, keyStr: string) { key_to_asset_key: record.address, image, hotspot_infos: { - iot: snakeCaseKeys(record?.iotHotspotInfo?.dataValues), - mobile: snakeCaseKeys(record?.mobileHotspotInfo?.dataValues), + iot: record?.iot_hotspot_info, + mobile: record?.mobile_hotspot_info, }, - entity_key_b64: record?.entityKey.toString("base64"), - key_serialization: record?.keySerialization, + entity_key_b64: record?.entity_key.toString("base64"), + key_serialization: record?.key_serialization, entity_key_str: keyStr, attributes: [ keyStr && Address.isValid(keyStr) @@ -189,18 +189,18 @@ function generateAssetJson(record: KeyToAsset, keyStr: string) { { trait_type: "entity_key_string", value: keyStr }, { trait_type: "entity_key", - value: record?.entityKey?.toString("base64"), + value: record?.entity_key?.toString("base64"), }, { trait_type: "rewardable", value: true }, { trait_type: "networks", value: [ - record?.iotHotspotInfo && "iot", - record?.mobileHotspotInfo && "mobile", + record?.iot_hotspot_info && "iot", + record?.mobile_hotspot_info && "mobile", ].filter(truthy), }, - ...locationAttributes("iot", record?.iotHotspotInfo), - ...locationAttributes("mobile", record?.mobileHotspotInfo), + ...locationAttributes("iot", record?.iot_hotspot_info), + ...locationAttributes("mobile", record?.mobile_hotspot_info), ], }; } @@ -218,7 +218,7 @@ const getHotspotByKeyToAsset = async (request, reply) => { return reply.code(404); } - const { entityKey, keySerialization } = record; + const { entity_key: entityKey, key_serialization: keySerialization } = record; const keyStr = decodeEntityKey(entityKey, { [keySerialization]: {} }); const assetJson = generateAssetJson(record, keyStr!); @@ -255,7 +255,7 @@ server.get<{ Params: { eccCompact: string } }>( const record = await KeyToAsset.findOne({ where: { - entityKey: bs58.decode(eccCompact), + entity_key: bs58.decode(eccCompact), }, include: [IotHotspotInfo, MobileHotspotInfo], }); @@ -269,25 +269,6 @@ server.get<{ Params: { eccCompact: string } }>( } ); -function snakeCaseKeys(obj: any): any { - if (obj instanceof Array) { - return obj.map((item) => snakeCaseKeys(item)); - } else if (obj instanceof Object) { - const snakeCasedObject: { [key: string]: any } = {}; - for (const key in obj) { - if (obj.hasOwnProperty(key)) { - const snakeCasedKey = key - .replace(/([A-Z])/g, (match) => `_${match.toLowerCase()}`) - .replace(/^_/, ""); // Remove leading underscore - snakeCasedObject[snakeCasedKey] = snakeCaseKeys(obj[key]); - } - } - return snakeCasedObject; - } else { - return obj; - } -} - function locationAttributes( name: string, info: MobileHotspotInfo | IotHotspotInfo | undefined diff --git a/packages/metadata-service/src/model.ts b/packages/metadata-service/src/model.ts index 3ff41cd4b..d088bf365 100644 --- a/packages/metadata-service/src/model.ts +++ b/packages/metadata-service/src/model.ts @@ -56,8 +56,8 @@ export class MobileHotspotInfo extends Model { declare city: string; declare state: string; declare country: string; - declare isActive: boolean; - declare deviceType: string; + declare is_active: boolean; + declare device_type: string; } MobileHotspotInfo.init( { @@ -69,14 +69,14 @@ MobileHotspotInfo.init( city: DataTypes.STRING, state: DataTypes.STRING, country: DataTypes.STRING, - isActive: DataTypes.BOOLEAN, - deviceType: DataTypes.JSONB, + is_active: DataTypes.BOOLEAN, + device_type: DataTypes.JSONB, location: DataTypes.DECIMAL.UNSIGNED, - dcOnboardingFeePaid: DataTypes.DECIMAL.UNSIGNED, + dc_onboarding_fee_paid: DataTypes.DECIMAL.UNSIGNED, }, { sequelize, - modelName: "mobileHotspotInfo", + modelName: "mobile_hotspot_info", tableName: "mobile_hotspot_infos", underscored: true, timestamps: false, @@ -90,7 +90,7 @@ export class IotHotspotInfo extends Model { declare city: string; declare state: string; declare country: string; - declare isActive: boolean; + declare is_active: boolean; } IotHotspotInfo.init( @@ -103,15 +103,15 @@ IotHotspotInfo.init( city: DataTypes.STRING, state: DataTypes.STRING, country: DataTypes.STRING, - isActive: DataTypes.BOOLEAN, + is_active: DataTypes.BOOLEAN, location: DataTypes.DECIMAL.UNSIGNED, - dcOnboardingFeePaid: DataTypes.DECIMAL.UNSIGNED, + dc_onboarding_fee_paid: DataTypes.DECIMAL.UNSIGNED, elevation: DataTypes.NUMBER, gain: DataTypes.NUMBER, }, { sequelize, - modelName: "iotHotspotInfo", + modelName: "iot_hotspot_info", tableName: "iot_hotspot_infos", underscored: true, timestamps: false, @@ -121,10 +121,10 @@ IotHotspotInfo.init( export class KeyToAsset extends Model { declare address: string; declare asset: string; - declare entityKey: Buffer; - declare mobileHotspotInfo?: MobileHotspotInfo; - declare iotHotspotInfo?: IotHotspotInfo; - declare keySerialization: string; + declare entity_key: Buffer; + declare mobile_hotspot_info?: MobileHotspotInfo; + declare iot_hotspot_info?: IotHotspotInfo; + declare key_serialization: string; } KeyToAsset.init( @@ -133,13 +133,13 @@ KeyToAsset.init( type: STRING, primaryKey: true, }, - entityKey: DataTypes.BLOB, + entity_key: DataTypes.BLOB, asset: STRING, - keySerialization: DataTypes.JSONB, + key_serialization: DataTypes.JSONB, }, { sequelize, - modelName: "keyToAsset", + modelName: "key_to_asset", tableName: "key_to_assets", underscored: true, timestamps: false,