Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Change snake to camel case
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Aug 2, 2020
1 parent 482e156 commit 3dadf22
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 64 deletions.
22 changes: 11 additions & 11 deletions src/_staart/helpers/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
users,
organizations,
memberships,
access_tokens,
api_keys,
accessTokens,
apiKeys,
} from "@prisma/client";
import { prisma } from "./prisma";
import { getUserById } from "../services/user.service";
Expand Down Expand Up @@ -56,7 +56,7 @@ const canUserUser = async (user: users, action: UserScopes, target: users) => {
* Whether an access token can perform an action for an organization
*/
const canAccessTokenUser = (
accessToken: access_tokens,
accessToken: accessTokens,
action: UserScopes,
target: users
) => {
Expand Down Expand Up @@ -163,7 +163,7 @@ const canUserSudo = async (user: users, action: SudoScopes) => {
* Whether an API key can perform an action for an organization
*/
const canApiKeyOrganization = (
apiKey: api_keys,
apiKey: apiKeys,
action: OrgScopes,
target: organizations
) => {
Expand All @@ -187,17 +187,17 @@ export const can = async (
targetType: "user" | "organization" | "membership" | "sudo",
target?: string | users | organizations | memberships
) => {
let requestFromType: "users" | "api_keys" | "access_tokens" = "users";
let requestFromType: "users" | "apiKeys" | "accessTokens" = "users";

/**
* First, we figure out what the first parameter is
* If it's a string, it can only be a user ID we'll convert to user
*/
if (typeof user === "object") {
if ((user as ApiKeyResponse).sub === Tokens.API_KEY) {
requestFromType = "api_keys";
requestFromType = "apiKeys";
} else if ((user as AccessTokenResponse).sub === Tokens.ACCESS_TOKEN) {
requestFromType = "access_tokens";
requestFromType = "accessTokens";
}
} else {
const result = await getUserById(user);
Expand Down Expand Up @@ -232,8 +232,8 @@ export const can = async (
}
}

if (requestFromType === "api_keys") {
const apiKeyDetails = await prisma.api_keys.findOne({
if (requestFromType === "apiKeys") {
const apiKeyDetails = await prisma.apiKeys.findOne({
where: { id: parseInt((user as ApiKeyResponse).id) },
});
if (!apiKeyDetails || !target) throw new Error(INVALID_TOKEN);
Expand All @@ -242,8 +242,8 @@ export const can = async (
action as OrgScopes,
target as organizations
);
} else if (requestFromType === "access_tokens") {
const accessTokenDetails = await prisma.access_tokens.findOne({
} else if (requestFromType === "accessTokens") {
const accessTokenDetails = await prisma.accessTokens.findOne({
where: { id: parseInt((user as ApiKeyResponse).id) },
});
if (!accessTokenDetails || !target) throw new Error(INVALID_TOKEN);
Expand Down
12 changes: 6 additions & 6 deletions src/_staart/helpers/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import {
removeFalsyValues,
} from "./utils";
import {
access_tokensCreateInput,
access_tokensUpdateInput,
accessTokensCreateInput,
accessTokensUpdateInput,
users,
api_keysCreateInput,
api_keysUpdateInput,
apiKeysCreateInput,
apiKeysUpdateInput,
} from "@prisma/client";
import { prisma } from "./prisma";
import {
Expand Down Expand Up @@ -153,7 +153,7 @@ export const twoFactorToken = (user: users) =>
* Generate an API key JWT
*/
export const apiKeyToken = (
apiKey: api_keysCreateInput | api_keysUpdateInput
apiKey: apiKeysCreateInput | apiKeysUpdateInput
) => {
const createApiKey = { ...removeFalsyValues(apiKey) };
delete createApiKey.createdAt;
Expand All @@ -174,7 +174,7 @@ export const apiKeyToken = (
* Generate an access token
*/
export const accessToken = (
accessToken: access_tokensCreateInput | access_tokensUpdateInput
accessToken: accessTokensCreateInput | accessTokensUpdateInput
) => {
const createAccessToken = { ...removeFalsyValues(accessToken) };
delete createAccessToken.createdAt;
Expand Down
6 changes: 3 additions & 3 deletions src/_staart/rest/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const github = {
clientSecret: GITHUB_CLIENT_SECRET,
redirectUri: getRedirectUri("github"),
authorizationUri: "https://github.com/login/oauth/authorize",
accessTokenUri: "https://github.com/login/oauth/access_token",
accessTokenUri: "https://github.com/login/oauth/accessToken",
scopes: ["read:user", "user:email"],
}),
callback: async (url: string, locals: Locals) => {
Expand All @@ -109,14 +109,14 @@ export const facebook = {
clientSecret: FACEBOOK_CLIENT_SECRET,
redirectUri: getRedirectUri("facebook"),
authorizationUri: "https://www.facebook.com/v3.3/dialog/oauth",
accessTokenUri: "https://graph.facebook.com/v3.3/oauth/access_token",
accessTokenUri: "https://graph.facebook.com/v3.3/oauth/accessToken",
scopes: ["email"],
}),
callback: async (url: string, locals: Locals) => {
const token = (await facebook.client.code.getToken(url)).accessToken;
const data = (
await axios.get(
`https://graph.facebook.com/v3.3/me?fields=name,email&access_token=${token}`
`https://graph.facebook.com/v3.3/me?fields=name,email&accessToken=${token}`
)
).data;
return loginWithOAuth2Service("facebook", data.name, data.email, locals);
Expand Down
28 changes: 14 additions & 14 deletions src/_staart/rest/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ import {
membershipsUpdateInput,
users,
MembershipRole,
api_keysSelect,
api_keysInclude,
api_keysOrderByInput,
api_keysWhereUniqueInput,
api_keysUpdateInput,
api_keysCreateInput,
apiKeysSelect,
apiKeysInclude,
apiKeysOrderByInput,
apiKeysWhereUniqueInput,
apiKeysUpdateInput,
apiKeysCreateInput,
webhooksCreateInput,
domainsSelect,
domainsInclude,
Expand Down Expand Up @@ -564,7 +564,7 @@ export const getAllOrganizationDataForUser = async (
id: parseInt(organizationId),
},
include: {
api_keys: true,
apiKeys: true,
domains: true,
memberships: true,
webhooks: true,
Expand Down Expand Up @@ -798,7 +798,7 @@ export const getOrganizationApiKeysForUser = async (
)
)
return paginatedResult(
await prisma.api_keys.findMany({
await prisma.apiKeys.findMany({
where: { organizationId: parseInt(organizationId) },
...queryParamsToSelect(queryParams),
}),
Expand All @@ -820,7 +820,7 @@ export const getOrganizationApiKeyForUser = async (
organizationId
)
)
return prisma.api_keys.findOne({ where: { id: parseInt(apiKeyId) } });
return prisma.apiKeys.findOne({ where: { id: parseInt(apiKeyId) } });
throw new Error(INSUFFICIENT_PERMISSION);
};

Expand Down Expand Up @@ -849,7 +849,7 @@ export const updateApiKeyForUser = async (
userId: string | ApiKeyResponse,
organizationId: string,
apiKeyId: string,
data: api_keysUpdateInput,
data: apiKeysUpdateInput,
locals: Locals
) => {
if (
Expand All @@ -860,7 +860,7 @@ export const updateApiKeyForUser = async (
organizationId
)
) {
const result = await prisma.api_keys.update({
const result = await prisma.apiKeys.update({
where: { id: parseInt(apiKeyId) },
data,
});
Expand All @@ -874,7 +874,7 @@ export const updateApiKeyForUser = async (
export const createApiKeyForUser = async (
userId: string | ApiKeyResponse,
organizationId: string,
apiKey: api_keysCreateInput,
apiKey: apiKeysCreateInput,
locals: Locals
) => {
if (
Expand All @@ -887,7 +887,7 @@ export const createApiKeyForUser = async (
) {
apiKey.jwtApiKey = randomString({ length: 20 });
apiKey.expiresAt = apiKey.expiresAt || new Date(TOKEN_EXPIRY_API_KEY_MAX);
const result = await prisma.api_keys.create({
const result = await prisma.apiKeys.create({
data: {
...apiKey,
organization: {
Expand Down Expand Up @@ -918,7 +918,7 @@ export const deleteApiKeyForUser = async (
organizationId
)
) {
const result = await prisma.api_keys.delete({
const result = await prisma.apiKeys.delete({
where: { id: parseInt(apiKeyId) },
});
queueWebhook(organizationId, Webhooks.DELETE_API_KEY, apiKeyId);
Expand Down
28 changes: 14 additions & 14 deletions src/_staart/rest/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ import {
membershipsInclude,
membershipsOrderByInput,
membershipsWhereUniqueInput,
access_tokensSelect,
access_tokensInclude,
access_tokensOrderByInput,
access_tokensWhereUniqueInput,
access_tokensUpdateInput,
access_tokensCreateInput,
accessTokensSelect,
accessTokensInclude,
accessTokensOrderByInput,
accessTokensWhereUniqueInput,
accessTokensUpdateInput,
accessTokensCreateInput,
sessionsSelect,
sessionsInclude,
sessionsOrderByInput,
Expand Down Expand Up @@ -229,7 +229,7 @@ export const getAllDataForUser = async (
where: { id: parseInt(userId) },
include: {
emails: true,
access_tokens: true,
accessTokens: true,
approved_locations: true,
backup_codes: true,
identities: true,
Expand Down Expand Up @@ -322,7 +322,7 @@ export const getUserAccessTokensForUser = async (
await can(tokenUserId, UserScopes.READ_USER_ACCESS_TOKENS, "user", userId)
)
return paginatedResult(
await prisma.access_tokens.findMany({
await prisma.accessTokens.findMany({
where: { userId: parseInt(userId) },
...queryParamsToSelect(queryParams),
}),
Expand All @@ -339,7 +339,7 @@ export const getUserAccessTokenForUser = async (
if (
await can(tokenUserId, UserScopes.READ_USER_ACCESS_TOKENS, "user", userId)
)
return prisma.access_tokens.findOne({
return prisma.accessTokens.findOne({
where: { id: parseInt(accessTokenId) },
});
throw new Error(INSUFFICIENT_PERMISSION);
Expand All @@ -349,13 +349,13 @@ export const updateAccessTokenForUser = async (
tokenUserId: string,
userId: string,
accessTokenId: string,
data: access_tokensUpdateInput,
data: accessTokensUpdateInput,
locals: Locals
) => {
if (
await can(tokenUserId, UserScopes.UPDATE_USER_ACCESS_TOKENS, "user", userId)
)
return prisma.access_tokens.update({
return prisma.accessTokens.update({
where: { id: parseInt(accessTokenId) },
data,
});
Expand All @@ -365,7 +365,7 @@ export const updateAccessTokenForUser = async (
export const createAccessTokenForUser = async (
tokenUserId: string,
userId: string,
accessToken: access_tokensCreateInput,
accessToken: accessTokensCreateInput,
locals: Locals
) => {
if (
Expand All @@ -374,7 +374,7 @@ export const createAccessTokenForUser = async (
accessToken.jwtAccessToken = randomString({ length: 20 });
accessToken.expiresAt =
accessToken.expiresAt || new Date(TOKEN_EXPIRY_API_KEY_MAX);
return prisma.access_tokens.create({
return prisma.accessTokens.create({
data: { ...accessToken, user: { connect: { id: parseInt(userId) } } },
});
}
Expand All @@ -390,7 +390,7 @@ export const deleteAccessTokenForUser = async (
if (
await can(tokenUserId, UserScopes.DELETE_USER_ACCESS_TOKENS, "user", userId)
)
return prisma.access_tokens.delete({
return prisma.accessTokens.delete({
where: { id: parseInt(accessTokenId) },
});
throw new Error(INSUFFICIENT_PERMISSION);
Expand Down
14 changes: 7 additions & 7 deletions src/_staart/services/organization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import { prisma } from "../helpers/prisma";
import {
organizationsCreateInput,
organizationsUpdateInput,
api_keysCreateInput,
api_keysUpdateInput,
apiKeysCreateInput,
apiKeysUpdateInput,
domainsCreateInput,
organizations,
} from "@prisma/client";
Expand Down Expand Up @@ -177,26 +177,26 @@ export const getApiKeyLogs = async (apiKeyId: string, query: KeyValue) => {
/**
* Create an API key
*/
export const createApiKey = async (apiKey: api_keysCreateInput) => {
export const createApiKey = async (apiKey: apiKeysCreateInput) => {
apiKey.expiresAt = apiKey.expiresAt || new Date(TOKEN_EXPIRY_API_KEY_MAX);
apiKey.jwtApiKey = await apiKeyToken(apiKey);
return prisma.api_keys.create({ data: apiKey });
return prisma.apiKeys.create({ data: apiKey });
};

/**
* Update a user's details
*/
export const updateApiKey = async (
apiKeyId: string,
data: api_keysUpdateInput
data: apiKeysUpdateInput
) => {
const apiKey = await prisma.api_keys.findOne({
const apiKey = await prisma.apiKeys.findOne({
where: { id: parseInt(apiKeyId) },
});
if (!apiKey) throw new Error(RESOURCE_NOT_FOUND);
if (apiKey.jwtApiKey) await invalidateToken(apiKey.jwtApiKey);
data.jwtApiKey = await apiKeyToken({ ...apiKey, ...data });
return prisma.api_keys.update({ data, where: { id: parseInt(apiKeyId) } });
return prisma.apiKeys.update({ data, where: { id: parseInt(apiKeyId) } });
};

/**
Expand Down
Loading

0 comments on commit 3dadf22

Please sign in to comment.