Skip to content

Commit

Permalink
add a trycatch block
Browse files Browse the repository at this point in the history
  • Loading branch information
goemen committed Aug 27, 2024
1 parent 6fe6846 commit c591361
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
7 changes: 1 addition & 6 deletions admin-frontend/src/components/AnnouncementsPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,7 @@ import { NotificationService } from '../services/notificationService';
const announcementSearchStore = useAnnouncementSearchStore();
const { searchResults, isSearching, hasSearched, totalNum, pageSize } =
storeToRefs(announcementSearchStore);
watch(
() => searchResults.value,
() => {
console.log('searchResults changed', searchResults.value);
},
);
const announcementInDialog = ref<any>(undefined);
const confirmDialog = ref<typeof ConfirmationDialog>();
const isAnnouncementDialogVisible = ref<boolean>(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import ConfirmationDialog from '../util/ConfirmationDialog.vue';
import ApiService from '../../services/apiService';
import { useAnnouncementSearchStore } from '../../store/modules/announcementSearchStore';
import { useAnnouncementSelectionStore } from '../../store/modules/announcementSelectionStore';
import { ref, watch } from 'vue';
import { ref } from 'vue';
import { NotificationService } from '../../services/notificationService';
import { useRouter } from 'vue-router';
Expand All @@ -61,11 +61,6 @@ const { announcement } = defineProps<{
announcement: any;
}>();
watch(() => announcement, () => {
console.log('announcement changed', announcement);
});
console.log('announcement', announcement);
const announcementSearchStore = useAnnouncementSearchStore();
const confirmDialog = ref<typeof ConfirmationDialog>();
const isDeleting = ref<boolean>(false);
Expand Down
3 changes: 0 additions & 3 deletions admin-frontend/src/services/__tests__/apiService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AxiosError } from 'axios';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { AnnouncementStatus } from '../../types/announcements';
import ApiService from '../apiService';
import { de } from '@faker-js/faker';

//Mock the interceptor used by the ApiService so it no longer depends on
//HTTP calls to the backend.
Expand All @@ -22,8 +21,6 @@ vi.mock('file-saver', async () => {
return { saveAs: (...args) => mockSaveAs(...args) };
});

const originalWindow = {...window}

describe('ApiService', () => {
beforeEach(() => {});

Expand Down
18 changes: 18 additions & 0 deletions backend/src/external/services/s3-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ describe('S3Api', () => {
expect(res.status).toHaveBeenCalledWith(400);
});
});
describe('get most recent file fails', () => {
it('should return 400', async () => {
mockFindFirstOrThrow.mockResolvedValue({
attachment_file_id: faker.string.uuid(),
display_name: faker.lorem.word(),
});
const res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};

mockSend.mockRejectedValue(new Error('error'));

const fileId = 'fileId';
await downloadFile(res, fileId);
expect(res.status).toHaveBeenCalledWith(400);
});
});
it('should return 400', async () => {
mockFindFirstOrThrow.mockRejectedValue(new Error('error'));

Expand Down
35 changes: 21 additions & 14 deletions backend/src/external/services/s3-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@ import prisma from '../../v1/prisma/prisma-client';
import os from 'os';
import fs from 'fs';
import { logger } from '../../logger';
import { APP_ANNOUNCEMENTS_FOLDER, S3_BUCKET, S3_OPTIONS } from '../../constants/admin';

import {
APP_ANNOUNCEMENTS_FOLDER,
S3_BUCKET,
S3_OPTIONS,
} from '../../constants/admin';

const getMostRecentFile = async (s3Client: S3Client, key: string) => {
const response = await s3Client.send(
new ListObjectsCommand({
Bucket: S3_BUCKET,
Prefix: `${APP_ANNOUNCEMENTS_FOLDER}/${key}`,
}),
);
const sortedData: any = response.Contents.sort((a: any, b: any) => {
const modifiedDateA: any = new Date(a.LastModified);
const modifiedDateB: any = new Date(b.LastModified);
return modifiedDateB - modifiedDateA;
});
return sortedData[0];
try {
const response = await s3Client.send(
new ListObjectsCommand({
Bucket: S3_BUCKET,
Prefix: `${APP_ANNOUNCEMENTS_FOLDER}/${key}`,
}),
);
const sortedData: any = response.Contents.sort((a: any, b: any) => {
const modifiedDateA: any = new Date(a.LastModified);
const modifiedDateB: any = new Date(b.LastModified);
return modifiedDateB - modifiedDateA;
});
return sortedData[0];
} catch (error) {
return undefined;
}
};

export const getFile = async (key: string) => {
Expand Down

0 comments on commit c591361

Please sign in to comment.