Repository: kiwicom/mobile
Pull request #1040 (2018-08-29 12:01:04)

MMB: Add bookingId to boardingPass file name
Basically doing this so it will be easier to determine if of we have downloaded and stored boarding passes for the booking.


Patch:
diff --git a/app/MMB/src/scenes/tickets/boardingPassesPdf/BoardingPassPdf.js b/app/MMB/src/scenes/tickets/boardingPassesPdf/BoardingPassPdf.js
index aef5491b..e655747f 100644
--- a/app/MMB/src/scenes/tickets/boardingPassesPdf/BoardingPassPdf.js
+++ b/app/MMB/src/scenes/tickets/boardingPassesPdf/BoardingPassPdf.js
@@ -5,22 +5,31 @@ import { GeneralError } from '@kiwicom/mobile-shared';
 import { Translation } from '@kiwicom/mobile-localization';
 import { PdfViewAndStore } from '@kiwicom/mobile-pdf';
 
-export const getPdfFilenameFromUrl = (url: string) => {
+import BookingDetailContext from '../../../context/BookingDetailContext';
+
+export const getPdfFilenameFromUrl = (url: string, bookingId: number) => {
   const filename = url.slice(url.lastIndexOf('/') + 1);
   const isPdf = filename.includes('.pdf');
   if (!isPdf) {
     return null;
   }
-  return filename.slice(0, filename.indexOf('.pdf'));
+  return `${filename.slice(
+    0,
+    filename.indexOf('.pdf'),
+  )}-bookingId:${bookingId}`;
 };
 
-type Props = {|
-  +boardingPassUrl: ?string,
+type PropsWithContext = {|
+  ...Props,
+  +bookingId: number,
 |};
 
-const BoardingPassPdf = ({ boardingPassUrl }: Props) => {
+export const BoardingPassPdf = ({
+  boardingPassUrl,
+  bookingId,
+}: PropsWithContext) => {
   if (boardingPassUrl) {
-    const filename = getPdfFilenameFromUrl(boardingPassUrl);
+    const filename = getPdfFilenameFromUrl(boardingPassUrl, bookingId);
 
     if (filename) {
       return (
@@ -38,4 +47,14 @@ const BoardingPassPdf = ({ boardingPassUrl }: Props) => {
   );
 };
 
-export default BoardingPassPdf;
+type Props = {|
+  +boardingPassUrl: ?string,
+|};
+
+export default function BoardingPassPdfWithContext(props: Props) {
+  return (
+    <BookingDetailContext.Consumer>
+      {({ bookingId }) => <BoardingPassPdf {...props} bookingId={bookingId} />}
+    </BookingDetailContext.Consumer>
+  );
+}


Test patch:
diff --git a/app/MMB/src/scenes/tickets/boardingPassesPdf/__tests__/BoardingPassPdf.test.js b/app/MMB/src/scenes/tickets/boardingPassesPdf/__tests__/BoardingPassPdf.test.js
index 9116789e..f96785d5 100644
--- a/app/MMB/src/scenes/tickets/boardingPassesPdf/__tests__/BoardingPassPdf.test.js
+++ b/app/MMB/src/scenes/tickets/boardingPassesPdf/__tests__/BoardingPassPdf.test.js
@@ -5,7 +5,7 @@ import ShallowRenderer from 'react-test-renderer/shallow';
 
 const renderer = new ShallowRenderer();
 
-import BoardingPassPdf, { getPdfFilenameFromUrl } from '../BoardingPassPdf';
+import { getPdfFilenameFromUrl, BoardingPassPdf } from '../BoardingPassPdf';
 
 jest.mock('react-native-fetch-blob', () => {
   return {
@@ -24,13 +24,20 @@ jest.mock('react-native-fetch-blob', () => {
   };
 });
 
+const bookingId = 123456;
+
 describe('BoardingPassPdf', () => {
   it('renders', () => {
     const wrapper = renderer.render(
-      <BoardingPassPdf boardingPassUrl="https://somedomain.com/folder/subfolder/file.pdf" />,
+      <BoardingPassPdf
+        bookingId={bookingId}
+        boardingPassUrl="https://somedomain.com/folder/subfolder/file.pdf"
+      />,
     );
 
-    expect(wrapper.props.fileName).toBe('boardingPasses/file.pdf');
+    expect(wrapper.props.fileName).toBe(
+      'boardingPasses/file-bookingId:123456.pdf',
+    );
   });
 });
 
@@ -39,19 +46,21 @@ describe('getPdfFilenameFromUrl', () => {
     const url =
       'https://somedomainname.com/numbers1234/Vienna-Palma%2CMajorca_1234567890_5bafa3bd88fbf8e8c90a0a.pdf?v=12345676';
 
-    expect(getPdfFilenameFromUrl(url)).toBe(
-      'Vienna-Palma%2CMajorca_1234567890_5bafa3bd88fbf8e8c90a0a',
+    expect(getPdfFilenameFromUrl(url, bookingId)).toBe(
+      'Vienna-Palma%2CMajorca_1234567890_5bafa3bd88fbf8e8c90a0a-bookingId:123456',
     );
     const pdf = 'somePdf.pdf';
-    expect(getPdfFilenameFromUrl(pdf)).toBe('somePdf');
+    expect(getPdfFilenameFromUrl(pdf, bookingId)).toBe(
+      'somePdf-bookingId:123456',
+    );
   });
 
   it('should return null if given a URL not containing .pdf', () => {
     const url =
       'https://somedomainname.com/numbers1234/Vienna-Palma%2CMajorca_1234567890_5bafa3bd88fbf8e8c90a0a.doc?v=12345676';
 
-    expect(getPdfFilenameFromUrl(url)).toBe(null);
+    expect(getPdfFilenameFromUrl(url, bookingId)).toBe(null);
     const pdf = 'someNonPdf.xls';
-    expect(getPdfFilenameFromUrl(pdf)).toBe(null);
+    expect(getPdfFilenameFromUrl(pdf, bookingId)).toBe(null);
   });
 });