Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Hava <[email protected]>
  • Loading branch information
devpikachu committed Oct 26, 2023
1 parent aad6f56 commit fe29218
Show file tree
Hide file tree
Showing 46 changed files with 112 additions and 159 deletions.
2 changes: 1 addition & 1 deletion pages/api/acceptMentorship.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getSession } from "next-auth/react";
import { getUser } from "../../services";
import { updateRequest, getRequestByQuery } from "../../services/requests";
import { createRelationship } from "../../services/relationships";
import { sendMentorshipRequestAcceptedEmail } from "../../utils/email";
import { sendMentorshipRequestAcceptedEmail } from "../../services/email";
import { RequestStatusEnum } from "../../types/dbTablesEnums";

export default async function handler(req, res) {
Expand Down
2 changes: 1 addition & 1 deletion pages/api/auth/[...nextauth].js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import NextAuth from "next-auth";
import LinkedInProvider from "next-auth/providers/linkedin";
import GoogleProvider from "next-auth/providers/google";
import EmailProvider from "next-auth/providers/email";
import XataAdapter from "../../../services/authAdapter";
import XataAdapter from "../../../services/auth/adapter";
import {getUserByEmail} from "../../../services/users";

const providers = [];
Expand Down
2 changes: 1 addition & 1 deletion pages/api/cancelRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getSession } from "next-auth/react";
import { getRequestById, getUser } from "../../services";
import { updateRequest } from "../../services/requests";
import { RequestStatusEnum } from "../../types/dbTablesEnums";
import { sendMentorshipRequestCancelledEmail } from "../../utils/email";
import { sendMentorshipRequestCancelledEmail } from "../../services/email";

export default async function handler(req, res) {
const session = await getSession({ req });
Expand Down
2 changes: 1 addition & 1 deletion pages/api/closeMentorship.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import {
sendMentorMentorshipClosedEmail,
sendMenteeMentorshipClosedEmail,
} from "../../utils/email";
} from "../../services/email";

export default async function handler(req, res) {
const session = await getSession({ req });
Expand Down
2 changes: 1 addition & 1 deletion pages/api/contactMentee.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getSession } from "next-auth/react";
import { getRequestById, getUser } from "../../services";
import { sendMentorContactMenteeEmail } from "../../utils/email";
import { sendMentorContactMenteeEmail } from "../../services/email";

export default async function handler(req, res) {
const session = await getSession({ req });
Expand Down
1 change: 0 additions & 1 deletion pages/api/mentors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { getSession } from "next-auth/react";
import { getXataClient } from "../../../services/xata";
import { getUser } from "../../../services";
import { RequestStatusEnum } from "../../../types/dbTablesEnums";
import getUserHasSessionAndPublished from "../../../utils/api/get-user-has-session-and-published";

export default async function handler(req, res) {
const xata = getXataClient();
Expand Down
2 changes: 1 addition & 1 deletion pages/api/profile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getSession } from "next-auth/react";
import { getXataClient } from "../../services/xata";
import { sendPreferencesUpdatedEmail } from "../../utils/email";
import { sendPreferencesUpdatedEmail } from "../../services/email";

async function getByEmail(email) {
const xata = await getXataClient();
Expand Down
2 changes: 1 addition & 1 deletion pages/api/rejectMentorship.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getSession } from "next-auth/react";
import { getUser } from "../../services";
import { updateRequest, getRequestByQuery } from "../../services/requests";
import { IsRequestStatusValid } from "../../types/dbTablesEnums";
import { sendMentorshipRequestRejectedEmail } from "../../utils/email";
import { sendMentorshipRequestRejectedEmail } from "../../services/email";

export default async function handler(req, res) {
const session = await getSession({ req });
Expand Down
2 changes: 1 addition & 1 deletion pages/api/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getSession } from "next-auth/react";
import { getUser, getUserById } from "../../services";
import { getXataClient } from "../../services/xata";
import { RequestStatusEnum } from "../../types/dbTablesEnums";
import { sendMentorshipRequestedEmail } from "../../utils/email";
import { sendMentorshipRequestedEmail } from "../../services/email";
import { GENERAL_REQUEST_ERROR_MESSAGE } from "../../constants";

export default async function handler(req, res) {
Expand Down
2 changes: 1 addition & 1 deletion pages/api/testNotifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
sendMentorshipRequestAcceptedEmail,
sendMentorshipRequestCancelledEmail,
sendMentorshipRequestRejectedEmail,
} from "../../utils/email";
} from "../../services/email";
import { RequestStatusEnum } from "../../types/dbTablesEnums";

async function getByEmail(email) {
Expand Down
104 changes: 44 additions & 60 deletions services/authAdapter.js → services/auth/adapter.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import {getXataClient} from "./xata";
import {sendNewUserCreatedEmail} from "../utils/email";
import {getXataClient} from "../xata";
import {sendNewUserCreatedEmail} from "../email";
import {createUser} from "../users";
import {createProfile, getProfileByEmail, getProfileByExternalId, getProfileByUserId} from "../profiles";
import {createMentor} from "../mentors";

export default function XataAdapter(client, options = {}) {
function convertProfile(profile) {
return {
id: profile.user.id,
name: profile.name,
image: "",
email: profile.user.email,
emailVerified: null,
published: profile.user.published,
};
}

export default function XataAdapter() {
return {
async createUser(user) {
console.log("createUser: creating user", user.email);

const xata = getXataClient();

const createdUser = await xata.db.users.create({
email: user.email,
roles: ["mentor", "mentee"],
});

await xata.db.profiles.create({
user: createdUser.id,
name: user.name,
});
const createdUser = await createUser({ email: user.email });
await createProfile({ id: createdUser.id, name: user.name });
// TODO: Only create a mentors entry if the user is a mentor
await createMentor({ id: createdUser.id });

// send email to request new user verification
sendNewUserCreatedEmail(createdUser.id, user.email)
// TODO: Send user verification email
// sendNewUserCreatedEmail(createdUser.id, user.email)

// returns AdapterUser
return {
id: createdUser.id,
name: user.name,
Expand All @@ -33,73 +39,55 @@ export default function XataAdapter(client, options = {}) {
},

async getUser(id) {
console.log("getUser: searching for user", user.id);
console.log("getUser: searching for user", id);

const xata = getXataClient();

const profile = await xata.db.profiles
.select(["*", "user.*"])
.filter({'user.id': id})
.getFirst();
const profile = await getProfileByUserId(id);

if (!profile) {
console.log("getUser: user not found");
return null;
}

return {
id: profile.user.id,
name: profile.name,
image: "",
email: profile.user.email,
emailVerified: null,
published: profile.user.published,
};
return convertProfile(profile);
},

async getUserByEmail(email) {
console.log("getUserByEmail: searching for user", email);

const xata = getXataClient();

const profile = await xata.db.profiles
.select(["*", "user.*"])
.filter({'user.email': email})
.getFirst();
const profile = await getProfileByEmail(email);

if (!profile) {
console.log("getUserByEmail: user not found");
return null;
}

return {
id: profile.user.id,
name: profile.name,
image: "",
email: profile.user.email,
emailVerified: null,
published: profile.user.published,
};
return convertProfile(profile);
},

async getUserByAccount({provider, providerAccountId}) {
console.log("getUserByAccount: searching for user", provider, providerAccountId);

const xata = getXataClient();

const profile = await xata.db.profiles
.select(["*", "user.*"])
.filter({
'user.provider': provider,
'user.externalId': providerAccountId
})
.getFirst();
const profile = await getProfileByExternalId({ provider, externalId: providerAccountId });

if (!profile) {
console.log("getUserByAccount: user not found");
return null;
}

return convertProfile(profile);
},

async updateUser(user) {
console.log("updateUser: updating user", user.id);

const xata = getXataClient();

// TODO: Do we need to update the user?
const profile = await xata.db.profiles
.select(["*", "user.*"])
.filter({'user.id': user.id})
.getFirst();

return {
id: profile.user.id,
name: profile.name,
Expand All @@ -110,16 +98,12 @@ export default function XataAdapter(client, options = {}) {
};
},

async updateUser(user) {
throw new Error("Not implemented");
},

async deleteUser(id) {
throw new Error("Not implemented");
},

async linkAccount(account) {
console.log("linkAccount: linking account", account);
console.log("linkAccount: linking account", account.provider, account.providerAccountId);

const xata = getXataClient();

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<mj-head>
<mj-include path="./style.mjml" />
<mj-include path="style.mjml" />
<mj-title>[[emailTitle]]</mj-title>
<mj-font name="Roboto" href="https://fonts.googleapis.com/css?family=Roboto:300,500"></mj-font>
<mj-attributes>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<mjml>
<mj-include path="./head.mjml" />
<mj-include path="head.mjml" />
<mj-body>
<mj-include path="./header.mjml" />
<mj-include path="header.mjml" />
<mj-section>
<mj-column width="100%">
<mj-text>
[[NOTIFICATION_BODY]]
</mj-text>
</mj-column>
</mj-section>
<mj-include path="./footer-madalina-tupu-team.mjml" />
<mj-include path="./footer.mjml" />
<mj-include path="footer-madalina-tupu-team.mjml" />
<mj-include path="footer.mjml" />
</mj-body>
</mjml>
9 changes: 9 additions & 0 deletions services/mentors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {getXataClient} from "./xata";

const xata = getXataClient();

export async function createMentor({ id }) {
return await xata.db.mentors.create({
user: id,
});
}
34 changes: 34 additions & 0 deletions services/profiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {getXataClient} from "./xata";

const xata = getXataClient();

export async function createProfile({id, name}) {
return await xata.db.profiles.create({
user: id,
name: name,
});
}

export async function getProfileByUserId(id) {
return await xata.db.profiles
.select(["*", "user.*"])
.filter({'user.id': id})
.getFirst();
}

export async function getProfileByEmail(email) {
return await xata.db.profiles
.select(["*", "user.*"])
.filter({'user.email': email})
.getFirst();
}

export async function getProfileByExternalId({ provider, externalId }) {
return await xata.db.profiles
.select(["*", "user.*"])
.filter({
'user.provider': provider,
'user.externalId': externalId
})
.getFirst();
}
33 changes: 11 additions & 22 deletions services/users.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
import { getXataClient } from "./xata";
import {getXataClient} from "./xata";

export async function getUser(session) {
const xata = getXataClient();
return xata.db.users.filter("email", session.user.email).getFirst();
}

export async function getUserByEmail(email) {
const xata = getXataClient();
return xata.db.users.filter("email", email).getFirst();
}
const xata = getXataClient();

export async function getUserById(id) {
const xata = getXataClient();
const user = await xata.db.users.read(id);
console.log("getUserById: found user with id", user.id, user);
return user;
export async function createUser({ email }) {
return await xata.db.users.create({
email: email,
// TODO: Don't add mentor role by default
roles: ["mentor", "mentee"],
});
}

export async function getRequestById(id) {
const xata = getXataClient();
return await xata.db.requests
.select(["*", "mentee.email", "mentee.name", "mentor.email", "mentor.name"])
.filter("id", id)
.getFirst();
}
export async function getUserByEmail(email) {
return await xata.db.users.filter("email", email).getFirst();
}
20 changes: 0 additions & 20 deletions utils/api/get-user-has-session-and-published.js

This file was deleted.

Loading

0 comments on commit fe29218

Please sign in to comment.