Skip to content

Commit

Permalink
Set building number on views to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Siilwyn committed Jun 5, 2024
1 parent 31b9b1d commit 40c50cf
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 30 deletions.
46 changes: 23 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions src/server/helpers/parse-message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe("parseMessageAruba", () => {
})).toMatchInlineSnapshot(`
{
"access_point_name": "A-23-0-007",
"building_number": 23,
"building_number": "23",
"device_count": 3,
"updated_at": "2023-03-21T12:01:20.000Z",
}
Expand All @@ -41,7 +41,22 @@ describe("parseMessageAruba", () => {
"from name with dashes"
)
.toContain({
"building_number": 66,
"building_number": "66",
});

expect(
parseMessageAruba({
timestamp: "1679400080000",
decodedValue: {
name: "AP-08-00-01-350-03",
site: "",
status: "Up",
}
}),
"start single digits with zero"
)
.toContain({
"building_number": "08",
});

expect(
Expand Down
8 changes: 7 additions & 1 deletion src/server/helpers/parse-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ export function parseMessageAruba(
"updated_at": new Date(Number(timestamp)).toISOString(),
"access_point_name": decodedValue.name,
"device_count": decodedValue.client_count,
"building_number": Number(String(decodedValue.name).split("-").at(1)) || null,
"building_number": parseBuildingNumber({ name: decodedValue.name as string }),
};
}

function parseBuildingNumber({ name }: { name: string }) {
const rawBuildingNumber = Number(name.split("-").at(1));
return rawBuildingNumber
? String(rawBuildingNumber).padStart(2, "0")
: null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
do $$
declare buildings_latest_states_def text;
declare spaces_latest_states_def text;
declare exec_text_one text;
declare exec_text_two text;
begin
buildings_latest_states_def := pg_get_viewdef('buildings_latest_states');
drop materialized view buildings_latest_states;
spaces_latest_states_def := pg_get_viewdef('spaces_latest_states');
drop materialized view spaces_latest_states;

alter table "public"."access_points_latest_states" alter column "building_number" set data type text using "building_number"::text;

exec_text_one := format('create view buildings_latest_states as %s',
buildings_latest_states_def);
execute exec_text_one;
exec_text_two := format('create view spaces_latest_states as %s',
spaces_latest_states_def);
execute exec_text_two;
end $$;
8 changes: 4 additions & 4 deletions supabase/seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ insert into
device_count
)
values
(now(), 'A-21-0-034', 21, '21.01', 78),
(now(), 'A-32-0-043', 32, '32.02', 58),
(now(), 'A-36-0-025', 36, null, 54),
(now(), 'A-32-0-029', 32, null, 46);
(now(), 'A-21-0-034', '21', '21.01', 78),
(now(), 'A-32-0-043', '32', '32.02', 58),
(now(), 'A-36-0-025', '36', null, 54),
(now(), 'A-32-0-029', '32', null, 46);

0 comments on commit 40c50cf

Please sign in to comment.