Skip to content

Commit

Permalink
Merge pull request #113 from ufabc-next/fix/bugs-sig-facebook
Browse files Browse the repository at this point in the history
Ajuste para devolver login facebook + Correçoes na tabela de alunos
  • Loading branch information
Joabesv authored Feb 10, 2025
2 parents 5289c40 + a96f756 commit ede0191
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 34 deletions.
2 changes: 1 addition & 1 deletion apps/core/src/models/Student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const studentSchema = new Schema(
{
ra: { type: Number },
login: { type: String, required: true },
aluno_id: { type: Number, required: true, default: null },
aluno_id: { type: Number, required: false },
cursos: [coursesSchema],
year: { type: Number, required: false },
quad: {
Expand Down
1 change: 1 addition & 0 deletions apps/core/src/routes/autohooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const PUBLIC_ROUTES = [
'/entities/students',
'/public/stats/components',
'/users/check-email',
'/users/facebook/email',
];

const isPublicRoute = (url: string): boolean => {
Expand Down
7 changes: 6 additions & 1 deletion apps/core/src/routes/entities/students/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type CreateStudent = {
type UpdateStudent = {
ra: number;
login: string;
studentId: number;
studentId: number | null | undefined;
};

export async function createOrInsert({
Expand All @@ -117,6 +117,7 @@ export async function createOrInsert({
graduations,
}: CreateStudent) {
const season = currentQuad();

const student = await StudentModel.findOneAndUpdate(
{
aluno_id: studentId,
Expand All @@ -141,6 +142,10 @@ export async function update({ ra, login, studentId }: UpdateStudent) {
return null;
}

if (student.aluno_id) {
return student;
}

student.aluno_id = studentId;

await student.save();
Expand Down
79 changes: 53 additions & 26 deletions apps/core/src/routes/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
import {
confirmUserSchema,
deactivateUserSchema,
getFacebookUserEmailSchema,
loginFacebookSchema,
resendEmailSchema,
validateUserEmailSchema,
} from '@/schemas/user.js';
Expand Down Expand Up @@ -42,32 +44,57 @@ const plugin: FastifyPluginAsyncZodOpenApi = async (app) => {
return userInfo;
});

// app.post(
// '/facebook',
// { schema: loginFacebookSchema },
// async (request, reply) => {
// const user = await UserModel.findOne({
// ra: request.body.ra,
// 'oauth.emailFacebook': request.body.email,
// });

// if (!user) {
// return reply.notFound('User not found');
// }

// const jwtToken = app.jwt.sign(
// {
// studentId: user._id.toJSON(),
// active: user.active,
// confirmed: user.confirmed,
// email: user.email,
// },
// { expiresIn: '2d' },
// );

// return { success: true, token: jwtToken };
// },
// );
app.get(
'/facebook/email',
{ schema: getFacebookUserEmailSchema },
async (request, reply) => {
const { ra } = request.query;

const user = await UserModel.findOne({
ra,
}).lean();

if (!user) {
return reply.notFound('Usuario não encontrado');
}

if (!user.oauth?.emailFacebook || !user.oauth.email) {
return reply.badRequest('Usuario nao possui conta do facebook');
}

return {
email: user.email,
};
},
);

app.post(
'/facebook',
{ schema: loginFacebookSchema },
async (request, reply) => {
const user = await UserModel.findOne({
ra: request.body.ra,
email: request.body.email,
'oauth.facebook': { $exists: true },
});

if (!user) {
return reply.notFound('Usuario não encontrado');
}

const jwtToken = app.jwt.sign(
{
studentId: user._id.toJSON(),
active: user.active,
confirmed: user.confirmed,
email: user.email,
},
{ expiresIn: '2d' },
);

return { success: true, token: jwtToken };
},
);

app.post('/resend', { schema: resendEmailSchema }, async (request, reply) => {
const user = await UserModel.findOne({
Expand Down
8 changes: 4 additions & 4 deletions apps/core/src/schemas/entities/students.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { FastifyZodOpenApiSchema } from 'fastify-zod-openapi';
const tags = ['Students'];

const listMatriculaStudentSchema = z.object({
studentId: z.number().int().nullable(),
studentId: z.number().int().nullish(),
graduations: z
.object({
courseId: z.number().int(),
Expand Down Expand Up @@ -68,7 +68,7 @@ export const listStudentSchema = {
content: {
'application/json': {
schema: z.object({
studentId: z.number().int(),
studentId: z.number().int().nullish(),
login: z.string(),
}),
},
Expand Down Expand Up @@ -108,10 +108,10 @@ export const updateStudentSchema = {
login: z.string().openapi({
example: 'john.doe',
}),
studentId: z.number().int(),
studentId: z.number().int().nullable(),
}),
response: {
204: {
200: {
content: {
'application/json': {
schema: z.object({
Expand Down
43 changes: 41 additions & 2 deletions apps/core/src/schemas/user.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { FastifyZodOpenApiSchema } from 'fastify-zod-openapi';
import { z } from 'zod';

const tags = ['User'];

export const deactivateUserSchema = {
tags: ['User'],
tags,
response: {
200: {
content: {
Expand All @@ -29,7 +31,7 @@ export const deactivateUserSchema = {
} satisfies FastifyZodOpenApiSchema;

export const resendEmailSchema = {
tags: ['User'],
tags,
response: {
200: {
content: {
Expand Down Expand Up @@ -81,6 +83,7 @@ export const confirmUserSchema = {
} satisfies FastifyZodOpenApiSchema;

export const validateUserEmailSchema = {
tags,
querystring: z.object({
ra: z.string().describe('Student ID (RA) of the user to validate'),
}),
Expand Down Expand Up @@ -109,3 +112,39 @@ export const validateUserEmailSchema = {
},
},
} satisfies FastifyZodOpenApiSchema;

export const getFacebookUserEmailSchema = {
tags,
querystring: z.object({
ra: z.string(),
}),
response: {
400: {
content: {
'application/json': {
schema: z.object({
message: z.string(),
}),
},
},
},
404: {
content: {
'application/json': {
schema: z.object({
message: z.string(),
}),
},
},
},
200: {
content: {
'application/json': {
schema: z.object({
email: z.string().email(),
}),
},
},
},
},
} satisfies FastifyZodOpenApiSchema;

0 comments on commit ede0191

Please sign in to comment.