async function handleS3Event()

in lambda/publish-todays-curation/src/main.ts [21:64]


async function handleS3Event(
	event: S3Event,
	contentPrefix: string,
	staticBucketName: string,
	fastlyApiKey: string,
): Promise<void> {
	const toWaitFor = event.Records.map((rec) => {
		if (rec.s3.bucket.name !== staticBucketName) {
			console.log(`Event was for bucket ${rec.s3.bucket.name}, ignoring`);
			return;
		}

		if (rec.eventName != 'ObjectCreated:Put') {
			console.log(
				`Event was for ${rec.eventName} not ObjectCreated:Put, ignoring`,
			);
			return;
		}

		const targetPath = decodeURIComponent(rec.s3.object.key);
		const info = checkCurationPath(targetPath);
		if (info) {
			if (doesCurationPathMatch(info, Today)) {
				console.log(
					`Detected an update of today's curation data for ${info.edition}/${info.front}, redeploying`,
				);
				return activateCuration(
					info,
					contentPrefix,
					staticBucketName,
					fastlyApiKey,
				);
			} else {
				console.log(
					`Detected an update of curation data for ${info.edition}/${info.front} on ${info.year}-${info.month}-${info.day}`,
				);
			}
		} else {
			console.log(`Event was for unrecognised path ${targetPath}`);
		}
	});

	await Promise.all(toWaitFor.filter((maybePromise) => !!maybePromise));
}