Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/s3/src/plugins/multipartParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ declare module "fastify" {
}

const plugin = async (fastify: FastifyInstance) => {
if (!fastify.hasContentTypeParser("multipart")) {
fastify.addContentTypeParser("multipart", (req, _payload, done) => {
fastify.addContentTypeParser("*", (req, _payload, done) => {
const contentType = req.headers["content-type"] || "";

if (contentType.includes("multipart")) {
if (
req.config.graphql?.enabled &&
req.routeOptions.url?.startsWith(req.config.graphql.path as string)
Expand All @@ -24,8 +26,8 @@ const plugin = async (fastify: FastifyInstance) => {
} else {
processMultipartFormData(req, _payload, done);
}
});
}
}
});
};

export default fastifyPlugin(plugin);
86 changes: 86 additions & 0 deletions packages/user/src/model/users/graphql/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GraphQLFileUpload, Multipart } from "@prefabs.tech/fastify-s3";
import { mercurius } from "mercurius";
import EmailVerification, {
EmailVerificationClaim,
Expand Down Expand Up @@ -306,6 +307,91 @@ const Mutation = {
return mercuriusError;
}
},
uploadPhoto: async (
parent: unknown,
arguments_: {
photo: {
file: GraphQLFileUpload;
};
},
context: MercuriusContext,
) => {
const { app, config, database, dbSchema, reply, user } = context;
const { photo } = arguments_;

const service = getUserService(config, database, dbSchema);

if (!user) {
return new mercurius.ErrorWithProps("unauthorized", {}, 401);
}

if (!photo) {
throw new CustomApiError({
message: "Missing photo file in the request body",
name: "ERROR_FILE_MISSING",
statusCode: 422,
});
}

try {
const fileData = photo.file.createReadStream();

const fileToUpload: Multipart = {
...photo.file,
data: fileData,
limit: false,
};

const file = await service.uploadPhoto(fileToUpload, user.id, user.id);

const updatedUser = await service.update(user.id, {
...(file && {
photoId: file.id as number,
}),
});

if (user.photoId && user.photoId !== updatedUser.photoId) {
await service.fileService.delete(user.photoId);
}

const request = reply.request;

request.user = updatedUser;

if (request.config.user.features?.profileValidation?.enabled) {
await request.session?.fetchAndSetClaim(
new ProfileValidationClaim(),
createUserContext(undefined, request),
);
}

if (request.config.user.features?.signUp?.emailVerification) {
await request.session?.fetchAndSetClaim(
EmailVerificationClaim,
createUserContext(undefined, request),
);
}

return updatedUser;
} catch (error) {
if (error instanceof CustomApiError) {
const mercuriusError = new mercurius.ErrorWithProps(error.name);

mercuriusError.statusCode = error.statusCode;

return mercuriusError;
}

app.log.error(error);

const mercuriusError = new mercurius.ErrorWithProps(
"Oops, Something went wrong",
);
mercuriusError.statusCode = 500;

return mercuriusError;
}
},
removePhoto: async (
parent: unknown,
arguments_: undefined,
Expand Down
2 changes: 2 additions & 0 deletions packages/user/src/model/users/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ const user = gql`
@auth(profileValidation: false, emailVerification: false)
changeEmail(email: String!): ChangeEmailResponse
@auth(profileValidation: false, emailVerification: false)
uploadPhoto(photo: Upload!): User
@auth(profileValidation: false, emailVerification: false)
removePhoto: User @auth(profileValidation: false, emailVerification: false)
}

Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.