@wepublish/api
Version:
API core for we.publish.
145 lines • 5.89 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateUserPassword = exports.updatePublicUser = exports.uploadPublicUserProfileImage = exports.updatePaymentProviderCustomers = void 0;
const tslib_1 = require("tslib");
const user_1 = require("../../db/user");
const api_1 = require("../../../../user-api/src");
const error_1 = require("../../error");
const validator_1 = require("../../validator");
const updatePaymentProviderCustomers = (paymentProviderCustomers, authenticateUser, userClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { user } = authenticateUser();
const updateUser = yield userClient.update({
where: { id: user.id },
data: {
paymentProviderCustomers: {
deleteMany: {
userId: user.id
},
createMany: {
data: paymentProviderCustomers
}
}
},
select: api_1.unselectPassword
});
if (!updateUser)
throw new error_1.NotFound('User', user.id);
return updateUser.paymentProviderCustomers;
});
exports.updatePaymentProviderCustomers = updatePaymentProviderCustomers;
/**
* Uploads the user profile image and returns the image and updated user
* @param uploadImageInput
* @param authenticateUser
* @param mediaAdapter
* @param imageClient
* @param userClient
*/
function uploadPublicUserProfileImage(uploadImageInput, authenticateUser, mediaAdapter, imageClient, userClient) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const { user } = authenticateUser();
// ignore
if (uploadImageInput === undefined) {
return null;
}
let newImage = null;
if (uploadImageInput) {
// upload new image
const { file, filename, title, description, tags, source, link, license, focalPoint } = uploadImageInput;
const _a = yield mediaAdapter.uploadImage(file), { id: newImageId } = _a, image = tslib_1.__rest(_a, ["id"]);
const prismaImgData = Object.assign(Object.assign({ id: newImageId }, image), { filename: filename !== null && filename !== void 0 ? filename : image.filename, title,
description,
tags,
source,
link,
license, focalPoint: {
create: focalPoint
} });
// update existing image
if (user.userImageID) {
newImage = yield imageClient.update({
where: {
id: user.userImageID
},
data: prismaImgData
});
}
else {
// create new image
newImage = yield imageClient.create({ data: prismaImgData });
}
// cleanup existing user profile from file system
if (newImage && user.userImageID) {
yield mediaAdapter.deleteImage(user.userImageID);
}
}
// eventually delete image, if upload is set to null
if (uploadImageInput === null && user.userImageID) {
// delete image from file system
yield mediaAdapter.deleteImage(user.userImageID);
// delete image from database
yield imageClient.delete({ where: { id: user.userImageID } });
}
return yield userClient.update({
where: {
id: user.id
},
data: {
userImageID: newImage === null || newImage === void 0 ? void 0 : newImage.id
},
select: api_1.unselectPassword
});
});
}
exports.uploadPublicUserProfileImage = uploadPublicUserProfileImage;
const updatePublicUser = ({ address, name, email, firstName, preferredName, flair, uploadImageInput }, authenticateUser, mediaAdapter, userClient, imageClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { user } = authenticateUser();
email = email ? email.toLowerCase() : email;
yield validator_1.Validator.createUser().parse({ address, name, email, firstName, preferredName });
if (email && user.email !== email) {
const userExists = yield userClient.findUnique({
where: { email: email }
});
if (userExists)
throw new error_1.EmailAlreadyInUseError();
}
// eventually upload user profile image
yield uploadPublicUserProfileImage(uploadImageInput, authenticateUser, mediaAdapter, imageClient, userClient);
const updateUser = yield userClient.update({
where: { id: user.id },
data: {
name,
firstName,
preferredName,
address: address
? {
upsert: {
create: address,
update: address
}
}
: undefined,
flair
},
select: api_1.unselectPassword
});
return updateUser;
});
exports.updatePublicUser = updatePublicUser;
const updateUserPassword = (password, passwordRepeated, hashCostFactor, authenticateUser, userClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const { user } = authenticateUser();
if (!user)
throw new error_1.NotAuthenticatedError();
if (password !== passwordRepeated) {
throw new error_1.UserInputError('password and passwordRepeat are not equal');
}
return userClient.update({
where: { id: user.id },
data: {
password: yield (0, user_1.hashPassword)(password, hashCostFactor)
},
select: api_1.unselectPassword
});
});
exports.updateUserPassword = updateUserPassword;
//# sourceMappingURL=user.public-mutation.js.map