async update()

in libs/newsletters-data-client/src/lib/draft-storage/S3DraftStorage/index.ts [151:196]


	async update(
		draft: DraftWithId,
		user: UserProfile,
	): Promise<
		| SuccessfulStorageResponse<DraftWithIdButNoMeta>
		| UnsuccessfulStorageResponse
	> {
		try {
			const existingDraft = await this.fetchDraft(draft.listId);

			if (!existingDraft?.meta) {
				console.warn(
					`Did not get valid meta data for ${this.listIdToKey(
						draft.listId,
					)} - creating new meta data`,
				);
			}

			const draftWithMetaAndId: DraftWithIdAndMeta = {
				...draft,
				meta: this.updateMeta(existingDraft?.meta ?? makeBlankMeta(), user),
			};

			await this.putDraftObject(draftWithMetaAndId);

			//fetching the data from s3 again to make sure the put worked. Is this necessary?
			const updatedDraft = await this.fetchDraft(draft.listId);

			if (!updatedDraft) {
				return {
					ok: false,
					message: `failed to update and retrieve ${
						draft.name ?? 'UNNAMED DRAFT'
					}.`,
					reason: StorageRequestFailureReason.S3Failure,
				};
			}

			return {
				ok: true,
				data: this.stripMeta(updatedDraft),
			};
		} catch (err) {
			return errorToResponse(err, draft.listId);
		}
	}