Skip to content

Commit

Permalink
fix: unique failed entities | feat: call server update on page unload
Browse files Browse the repository at this point in the history
  • Loading branch information
pcournut committed Jul 26, 2023
1 parent b6782b7 commit ed42b6a
Showing 1 changed file with 148 additions and 129 deletions.
277 changes: 148 additions & 129 deletions views/ScanScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,87 +28,128 @@ export const ScanScreen = ({ navigation, route }) => {
const [failedKentoEntities, setFailedKentoEntities] = useState<string[]>([]);

// Functions
const participantListUpdate = async (
kentoEntityIds: string[],
token: string,
accesses: [Access],
scanTerminal: string
) => {
const TIMEOUT = 25000; // Set your timeout value
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), TIMEOUT);

var myHeaders = new Headers();
myHeaders.append("Authorization", `Bearer ${token}`);

var formdata = new FormData();
formdata.append(
"scanned_entities",
`[${kentoEntityIds.map((item) => '"' + item + '"')}]`
);
formdata.append("scan_terminal", `${scanTerminal}`);
formdata.append(
"accesses",
`[${accesses.map(({ _id }) => '"' + _id + '"')}]`
);
formdata.append(
"last_query_unix_timestamp",
`${lastQueryUnixTimeStamp.toString()}`
);

var requestOptions: RequestInit = {
method: "POST",
headers: myHeaders,
body: formdata,
redirect: "follow",
const callUpdate = () => {
var changedKentoEntities = [];
for (const user of enrichedUsers) {
for (const kentoEntity of user.kentoEntities) {
if (kentoEntity.toUpdate) {
changedKentoEntities = changedKentoEntities.concat(kentoEntity._id);
kentoEntity.toUpdate = false;
}
}
}
participantListUpdate(
[...failedKentoEntities, ...changedKentoEntities],
route.params.token,
route.params.accesses,
route.params.scanTerminal
);
};

try {
const response = await fetch(
`${route.params.devEnvironment ? devEndpoint : prodEndpoint}/wf/participant-list-update`,
{ signal: controller.signal, ...requestOptions }
)
.catch((e) => {
if (e.name === 'AbortError') {
// If the request times out, add the entityIds to the failed list
setFailedKentoEntities([...failedKentoEntities, ...kentoEntityIds]);
throw new Error('Request timed out');
const participantListUpdate = async (
kentoEntityIds: string[],
token: string,
accesses: [Access],
scanTerminal: string
) => {
const TIMEOUT = 25000; // Set your timeout value
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), TIMEOUT);

var myHeaders = new Headers();
myHeaders.append("Authorization", `Bearer ${token}`);

var formdata = new FormData();
formdata.append(
"scanned_entities",
`[${kentoEntityIds.map((item) => '"' + item + '"')}]`
);
formdata.append("scan_terminal", `${scanTerminal}`);
formdata.append(
"accesses",
`[${accesses.map(({ _id }) => '"' + _id + '"')}]`
);
formdata.append(
"last_query_unix_timestamp",
`${lastQueryUnixTimeStamp.toString()}`
);

var requestOptions: RequestInit = {
method: "POST",
headers: myHeaders,
body: formdata,
redirect: "follow",
};

try {
const response = await fetch(
`${
route.params.devEnvironment ? devEndpoint : prodEndpoint
}/wf/participant-list-update`,
// Time-out debug URL
// "http://www.google.com:81/",
{ signal: controller.signal, ...requestOptions }
)
.catch((e) => {
if (e.name === "AbortError") {
// If the request times out, add the entityIds to the failed list
const newFailedKentoEntities = Array.from(
new Set([...failedKentoEntities, ...kentoEntityIds])
);
setFailedKentoEntities(newFailedKentoEntities);
throw new Error("Request timed out");
}

throw e;
})
.finally(() => clearTimeout(id));

if (!response.ok) {
// If server response is not ok, add the entityIds to the failed list
const newFailedKentoEntities = Array.from(
new Set([...failedKentoEntities, ...kentoEntityIds])
);
setFailedKentoEntities(newFailedKentoEntities);
throw new Error("Server response was not ok");
}

throw e;
})
.finally(() => clearTimeout(id));

if (!response.ok) {
// If server response is not ok, add the entityIds to the failed list
setFailedKentoEntities([...failedKentoEntities, ...kentoEntityIds]);
throw new Error('Server response was not ok');
}

const json = await response.json();
var newEnrichedUsers: EnrichedUser[] = Object.assign([], enrichedUsers);
if ("updated_entities_text" in json.response) {
const updatedEntities: [UpdatedEntity] = JSON.parse(
json.response.updated_entities_text
);
console.log(`Number of updated entities: ${updatedEntities.length}`);
if (updatedEntities.length > 0) {
console.log(JSON.stringify(updatedEntities));
for (const updatedEntity of updatedEntities) {
const userIndex = enrichedUsers.findIndex(
// (item: EnrichedUser) => item._id === updatedEntity.owner._id
(item: EnrichedUser) => item.email === updatedEntity.owner.email
);
if (userIndex !== -1) {
const kentoEntityIndex = enrichedUsers[
userIndex
].kentoEntities.findIndex(
(item: KentoEntity) => item._id === updatedEntity._id
const json = await response.json();
var newEnrichedUsers: EnrichedUser[] = Object.assign([], enrichedUsers);
if ("updated_entities_text" in json.response) {
const updatedEntities: [UpdatedEntity] = JSON.parse(
json.response.updated_entities_text
);
console.log(`Number of updated entities: ${updatedEntities.length}`);
if (updatedEntities.length > 0) {
console.log(JSON.stringify(updatedEntities));
for (const updatedEntity of updatedEntities) {
const userIndex = enrichedUsers.findIndex(
// (item: EnrichedUser) => item._id === updatedEntity.owner._id
(item: EnrichedUser) => item.email === updatedEntity.owner.email
);
if (kentoEntityIndex !== -1) {
newEnrichedUsers[userIndex].kentoEntities[
kentoEntityIndex
].isUsed = updatedEntity.scan_terminal !== undefined;
if (userIndex !== -1) {
const kentoEntityIndex = enrichedUsers[
userIndex
].kentoEntities.findIndex(
(item: KentoEntity) => item._id === updatedEntity._id
);
if (kentoEntityIndex !== -1) {
newEnrichedUsers[userIndex].kentoEntities[
kentoEntityIndex
].isUsed = updatedEntity.scan_terminal !== undefined;
} else {
const kentoEntity: KentoEntity = {
_id: updatedEntity._id,
access: updatedEntity.access,
owner: updatedEntity.owner._id,
owner_email: updatedEntity.owner.email,
scan_terminal: updatedEntity.scan_terminal,
isUsed:
updatedEntity.scan_terminal !== undefined &&
updatedEntity.scan_terminal.length > 0,
};
newEnrichedUsers[userIndex].kentoEntities.push(kentoEntity);
}
} else {
const kentoEntity: KentoEntity = {
_id: updatedEntity._id,
Expand All @@ -120,68 +161,46 @@ const participantListUpdate = async (
updatedEntity.scan_terminal !== undefined &&
updatedEntity.scan_terminal.length > 0,
};
newEnrichedUsers[userIndex].kentoEntities.push(kentoEntity);
const enrichedUser: EnrichedUser = {
// _id: updatedEntity.owner._id,
first_name:
"first_name" in updatedEntity.owner
? updatedEntity.owner.first_name
: updatedEntity.owner.email,
last_name:
"last_name" in updatedEntity.owner
? updatedEntity.owner.last_name
: "",
email: updatedEntity.owner.email,
kentoEntities: [kentoEntity],
};
newEnrichedUsers.push(enrichedUser);
}
} else {
const kentoEntity: KentoEntity = {
_id: updatedEntity._id,
access: updatedEntity.access,
owner: updatedEntity.owner._id,
owner_email: updatedEntity.owner.email,
scan_terminal: updatedEntity.scan_terminal,
isUsed:
updatedEntity.scan_terminal !== undefined &&
updatedEntity.scan_terminal.length > 0,
};
const enrichedUser: EnrichedUser = {
// _id: updatedEntity.owner._id,
first_name:
"first_name" in updatedEntity.owner
? updatedEntity.owner.first_name
: updatedEntity.owner.email,
last_name:
"last_name" in updatedEntity.owner
? updatedEntity.owner.last_name
: "",
email: updatedEntity.owner.email,
kentoEntities: [kentoEntity],
};
newEnrichedUsers.push(enrichedUser);
}
}
} else {
console.log(`Empty updated entities.`);
}
} else {
console.log(`Empty updated entities.`);
}
setEnrichedUsers(newEnrichedUsers);
// When a response is successful, remove the corresponding entityIds from the failed list
setFailedKentoEntities(failedKentoEntities.filter(id => !kentoEntityIds.includes(id)));
} catch (error) {
console.log("error", error);
}
};

setEnrichedUsers(newEnrichedUsers);
// When a response is successful, remove the corresponding entityIds from the failed list
const newFailedKentoEntities = Array.from(
new Set([...failedKentoEntities, ...kentoEntityIds])
);
setFailedKentoEntities(newFailedKentoEntities);
} catch (error) {
console.log("error", error);
}
};

// Server update
useEffect(() => {
const interval = setInterval(() => {
var changedKentoEntities = [];
for (const user of enrichedUsers) {
for (const kentoEntity of user.kentoEntities) {
if (kentoEntity.toUpdate) {
changedKentoEntities = changedKentoEntities.concat(kentoEntity._id);
kentoEntity.toUpdate = false;
}
}
}
participantListUpdate(
[...failedKentoEntities, ...changedKentoEntities],
route.params.token,
route.params.accesses,
route.params.scanTerminal
);
callUpdate();
}, 30000);
return () => clearInterval(interval);
return () => {
clearInterval(interval);
callUpdate();
};
}, [failedKentoEntities]);

return (
Expand Down

0 comments on commit ed42b6a

Please sign in to comment.