async create()

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


	async create(
		draft: DraftWithoutId,
		user: UserProfile,
	): Promise<
		| SuccessfulStorageResponse<DraftWithIdButNoMeta>
		| UnsuccessfulStorageResponse
	> {
		try {
			const nextId = await this.getNextId();
			await this.putDraftObject({
				...draft,
				creationTimeStamp: Date.now(),
				listId: nextId,
				meta: this.createNewMeta(user),
			});

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

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

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