Skip to content

Commit

Permalink
fix: handle create/edit subscriber when target DG does not exist (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
patriciareinoso authored Jan 27, 2025
1 parent e51146f commit 3f6bb50
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
9 changes: 5 additions & 4 deletions utils/createSubscriber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ export const createSubscriber = async ({
throw new Error("Subscriber already exists.");
}

const existingDeviceGroupResponse = await apiGetDeviceGroup(deviceGroupName, token);
var existingDeviceGroupData = await existingDeviceGroupResponse.json();
if (!existingDeviceGroupData){
throw new Error(`Device group ${deviceGroupName} cannot be found`);
}
const updateSubscriberResponse = await apiPostSubscriber(imsi, subscriberData, token);
if (!updateSubscriberResponse.ok) {
throw new Error(
`Error creating subscriber. Error code: ${updateSubscriberResponse.status}`,
);
}

const existingDeviceGroupResponse = await apiGetDeviceGroup(deviceGroupName, token);
var existingDeviceGroupData = await existingDeviceGroupResponse.json();

if (!existingDeviceGroupData["imsis"]) {
existingDeviceGroupData["imsis"] = [];
}
Expand Down
19 changes: 13 additions & 6 deletions utils/editSubscriber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,20 @@ export const editSubscriber = async ({
};

try {
var newDeviceGroupData = await getDeviceGroupData(newDeviceGroupName, token);
if (!newDeviceGroupData){
throw new Error(`Device group ${newDeviceGroupName} cannot be found`);
}
await updateSubscriber(subscriberData, token);
if (oldDeviceGroupName != newDeviceGroupName) {
var oldDeviceGroupData = await getDeviceGroupData(oldDeviceGroupName, token);
const index = oldDeviceGroupData["imsis"].indexOf(imsi);
oldDeviceGroupData["imsis"].splice(index, 1);
await updateDeviceGroupData(oldDeviceGroupName, oldDeviceGroupData, token);
var newDeviceGroupData = await getDeviceGroupData(newDeviceGroupName, token);
if (oldDeviceGroupName){
var oldDeviceGroupData = await getDeviceGroupData(oldDeviceGroupName, token);
if (oldDeviceGroupData){
const index = oldDeviceGroupData["imsis"].indexOf(imsi);
oldDeviceGroupData["imsis"].splice(index, 1);
await updateDeviceGroupData(oldDeviceGroupName, oldDeviceGroupData, token);
}
}
newDeviceGroupData["imsis"].push(imsi);
await updateDeviceGroupData(newDeviceGroupName, newDeviceGroupData, token);
}
Expand Down Expand Up @@ -83,7 +90,7 @@ const getDeviceGroupData = async (deviceGroupName: string, token: string) => {
const existingDeviceGroupResponse = await apiGetDeviceGroup(deviceGroupName, token);
var existingDeviceGroupData = await existingDeviceGroupResponse.json();

if (!existingDeviceGroupData["imsis"]) {
if (existingDeviceGroupData && !existingDeviceGroupData["imsis"]) {
existingDeviceGroupData["imsis"] = [];
}
return existingDeviceGroupData;
Expand Down

0 comments on commit 3f6bb50

Please sign in to comment.