Skip to content

Commit

Permalink
rely on sequlize snake case
Browse files Browse the repository at this point in the history
  • Loading branch information
bryzettler committed Nov 2, 2023
1 parent db6b85e commit dd0843a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 54 deletions.
55 changes: 18 additions & 37 deletions packages/metadata-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!);
});
Expand Down Expand Up @@ -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") {
Expand All @@ -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,
};
});
}
Expand All @@ -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"
}`;
Expand All @@ -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)
Expand All @@ -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),
],
};
}
Expand All @@ -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!);
Expand Down Expand Up @@ -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],
});
Expand All @@ -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
Expand Down
34 changes: 17 additions & 17 deletions packages/metadata-service/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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,
Expand Down

0 comments on commit dd0843a

Please sign in to comment.