[offers][feat] add add to user profile endpoint

pull/376/head
Stuart Long Chay Boon 2 years ago
parent fc27afc529
commit 9c9f625c53

@ -25,6 +25,23 @@ function Test() {
},
});
const addToUserProfileMutation = trpc.useMutation(['offers.profile.addToUserProfile'], {
onError(err: any) {
alert(err);
},
onSuccess(data) {
setCreatedData(JSON.stringify(data));
},
})
const handleLink = () => {
addToUserProfileMutation.mutate({
profileId: 'cl96stky5002ew32gx2kale2x',
token: 'afca11e436d21bde24543718fa957c6c625335439dc504f24ee35eae7b5ef1ba',
userId: 'cl97dl51k001e7iygd5v5gt58'
})
}
const handleClick = () => {
createMutation.mutate({
background: {
@ -525,6 +542,9 @@ function Test() {
<button type="button" onClick={handleUpdate}>
UPDATE!
</button>
<button type="button" onClick={handleLink}>
LINKKKK!
</button>
<button
className="text-danger-600"
type="button"

@ -1026,4 +1026,42 @@ export const offersProfileRouter = createRouter()
message: 'Invalid token.',
});
},
})
.mutation('addToUserProfile', {
input: z.object({
profileId: z.string(),
token: z.string(),
userId: z.string(),
}),
async resolve({ ctx, input }) {
const profile = await ctx.prisma.offersProfile.findFirst({
where: {
id: input.profileId,
},
});
const profileEditToken = profile?.editToken;
// To validate user editing, OP or correct user
// TODO: improve validation process
if (profileEditToken === input.token) {
return await ctx.prisma.offersProfile.update({
data: {
user: {
connect: {
id: input.userId
}
}
},
where: {
id: input.profileId
}
})
}
throw new trpc.TRPCError({
code: 'UNAUTHORIZED',
message: 'Invalid token.',
});
}
});

Loading…
Cancel
Save