-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2651 from guardian/discount-expiry-notifier-opera…
…tions-status-to-s3 Discount expiry notifier - save operations status to s3
- Loading branch information
Showing
8 changed files
with
1,144 additions
and
376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 47 additions & 1 deletion
48
handlers/discount-expiry-notifier/src/handlers/saveResults.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,47 @@ | ||
export const handler = () => {}; | ||
import { getIfDefined } from '@modules/nullAndUndefined'; | ||
import { uploadFileToS3 } from '../s3'; | ||
|
||
type ExpiringDiscountToProcess = { | ||
subName: string; | ||
firstName: string; | ||
paymentAmount: number; | ||
paymentFrequency: string; | ||
nextPaymentDate: string; | ||
}; | ||
|
||
type ExpiringDiscountProcessingAttempt = { | ||
status: string; | ||
}; | ||
|
||
type LambdaInput = { | ||
expiringDiscountsToProcess: ExpiringDiscountToProcess[]; | ||
expiringDiscountProcessingAttempts: ExpiringDiscountProcessingAttempt[]; | ||
}; | ||
|
||
export const handler = async (event: LambdaInput) => { | ||
const bucketName = getIfDefined<string>( | ||
process.env.S3_BUCKET, | ||
'S3_BUCKET environment variable not set', | ||
); | ||
|
||
const getCurrentDateString = (): string => { | ||
const now = new Date(); | ||
const year = now.getFullYear(); | ||
const month = String(now.getMonth() + 1).padStart(2, '0'); // Months are 0-based | ||
const day = String(now.getDate()).padStart(2, '0'); | ||
return `${year}-${month}-${day}`; | ||
}; | ||
|
||
const filePath = getCurrentDateString(); | ||
|
||
//TODO add a precheck to find out if the file exists already and either append or create a separate file | ||
await uploadFileToS3({ | ||
bucketName, | ||
filePath, | ||
content: JSON.stringify(event), | ||
}); | ||
|
||
return { | ||
filePath, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { | ||
GetObjectCommandInput, | ||
PutObjectCommandInput, | ||
} from '@aws-sdk/client-s3'; | ||
import { | ||
GetObjectCommand, | ||
PutObjectCommand, | ||
S3Client, | ||
} from '@aws-sdk/client-s3'; | ||
|
||
const s3Client = new S3Client({ region: 'eu-west-1' }); | ||
|
||
export const uploadFileToS3 = async ({ | ||
bucketName, | ||
filePath, | ||
content, | ||
}: { | ||
bucketName: PutObjectCommandInput['Bucket']; | ||
filePath: PutObjectCommandInput['Key']; | ||
content: PutObjectCommandInput['Body']; | ||
}) => { | ||
try { | ||
const command = new PutObjectCommand({ | ||
Bucket: bucketName, | ||
Key: filePath, | ||
Body: content, | ||
}); | ||
const response = await s3Client.send(command); | ||
return response; | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
}; | ||
|
||
export const getFileFromS3 = async ({ | ||
bucketName, | ||
filePath, | ||
}: { | ||
bucketName: GetObjectCommandInput['Bucket']; | ||
filePath: GetObjectCommandInput['Key']; | ||
}) => { | ||
try { | ||
const command = new GetObjectCommand({ | ||
Bucket: bucketName, | ||
Key: filePath, | ||
}); | ||
|
||
const response = await s3Client.send(command); | ||
const fileContent = response.Body?.transformToString(); | ||
|
||
if (!fileContent) { | ||
throw new Error('File is empty'); | ||
} | ||
|
||
return fileContent; | ||
} catch (error) { | ||
console.error(error); | ||
throw error; | ||
} | ||
}; |
Oops, something went wrong.