async update()

in libs/newsletters-data-client/src/lib/newsletter-storage/s3-newsletter-storage.ts [244:283]


	async update(
		listId: number,
		modifications: Partial<NewsletterData>,
		user: UserProfile,
	): Promise<
		| SuccessfulStorageResponse<NewsletterDataWithoutMeta>
		| UnsuccessfulStorageResponse
	> {
		const modificationError = this.getModificationError(modifications);

		if (modificationError) return modificationError;

		const newsletterToUpdate = await this.fetchNewsletter(listId);
		if (!newsletterToUpdate) {
			return {
				ok: false,
				message: `failed to read newsletter with id ${listId}`,
			};
		}
		const updatedNewsletter: NewsletterDataWithMeta = {
			...newsletterToUpdate,
			...modifications,
			meta: this.updateMeta(newsletterToUpdate.meta ?? makeBlankMeta(), user),
		};
		const identifier = `${updatedNewsletter.identityName}:${updatedNewsletter.listId}.json`;

		try {
			await this.putObject(updatedNewsletter, identifier);
			return {
				ok: true,
				data: this.stripMeta(updatedNewsletter),
			};
		} catch (err) {
			return {
				ok: false,
				message: `failed to update newsletter with id ${listId}`,
				reason: StorageRequestFailureReason.S3Failure,
			};
		}
	}