Skip to content

Commit

Permalink
adding more error catching
Browse files Browse the repository at this point in the history
  • Loading branch information
jue-henry committed Dec 13, 2024
1 parent b5611b6 commit 7c18179
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/api/db/models/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ export class ProjectModel {
v.filters.cameras?.includes(input.cameraId) ||
projectDeps.some((d) => v.filters.deployments?.includes(d))
) {
v.filters.cameras = v.filters.cameras?.filter((c) => c === input.cameraId);
v.filters.cameras = v.filters.cameras?.filter((c) => c !== input.cameraId);
v.filters.deployments = v.filters.deployments?.filter(
(d) => !projectDeps.includes(d),
);
Expand Down
9 changes: 6 additions & 3 deletions src/task/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export async function UpdateSerialNumber(task: TaskInput<gql.UpdateCameraSerialN

export async function DeleteCamera(task: TaskInput<gql.DeleteCameraInput>) {
const context = { user: { is_superuser: true, curr_project: task.projectId } as User };

console.log('CameraModel.deleteCameraConfig - input: ', task.config);
const errors = [];
try {
// Step 1: delete deployments from views
await ProjectModel.removeCameraFromViews(
Expand All @@ -31,7 +31,7 @@ export async function DeleteCamera(task: TaskInput<gql.DeleteCameraInput>) {
);

// Step3: delete images associated with this camera
await DeleteImagesByFilter({
const deleteRes = await DeleteImagesByFilter({
projectId: task.projectId,
config: {
filters: {
Expand All @@ -41,6 +41,9 @@ export async function DeleteCamera(task: TaskInput<gql.DeleteCameraInput>) {
type: 'DeleteImagesByFilter',
user: task.user,
});
if (deleteRes.errors) {
errors.push(...deleteRes.errors);
}
// Step 4: unregister camera
if (
(await CameraModel.getWirelessCameras({ _ids: [task.config.cameraId] }, context)).length > 0
Expand All @@ -50,5 +53,5 @@ export async function DeleteCamera(task: TaskInput<gql.DeleteCameraInput>) {
} catch (err) {
return { isOk: false, error: err };
}
return { isOk: true };
return { isOk: true, errors: errors };
}
17 changes: 13 additions & 4 deletions src/task/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ export async function DeleteImagesByFilter(task: TaskInput<gql.DeleteImagesByFil
{ filters: task.config.filters, limit: ImageModel.DELETE_IMAGES_BATCH_SIZE },
context,
);
const errors = [];

while (images.results.length > 0) {
const batch = images.results.map((image) => image._id);
await ImageModel.deleteImages({ imageIds: batch }, context);
const res = await ImageModel.deleteImages({ imageIds: batch }, context);
if (res.errors) {
errors.push(...res.errors);
}
if (images.hasNext) {
images = await ImageModel.queryByFilter(
{
Expand All @@ -32,7 +37,7 @@ export async function DeleteImagesByFilter(task: TaskInput<gql.DeleteImagesByFil
}
}

return { filters: task.config.filters };
return { filters: task.config.filters, errors: errors };
}

export async function DeleteImages(task: TaskInput<gql.DeleteImagesInput>) {
Expand All @@ -44,9 +49,13 @@ export async function DeleteImages(task: TaskInput<gql.DeleteImagesInput>) {
*/
const context = { user: { is_superuser: true, curr_project: task.projectId } as User };
const imagesToDelete = task.config.imageIds?.slice() ?? [];
const errors = [];
while (imagesToDelete.length > 0) {
const batch = imagesToDelete.splice(0, ImageModel.DELETE_IMAGES_BATCH_SIZE);
await ImageModel.deleteImages({ imageIds: batch }, context);
const res = await ImageModel.deleteImages({ imageIds: batch }, context);
if (res.errors) {
errors.push(...res.errors);
}
}
return { imageIds: task.config.imageIds };
return { imageIds: task.config.imageIds, errors: errors };
}

0 comments on commit 7c18179

Please sign in to comment.