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

Commit

Permalink
🐛 Convert int to string
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Aug 31, 2019
1 parent b452ac7 commit 63de116
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 48 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "staart-manager",
"version": "1.1.11",
"version": "1.1.12",
"main": "index.js",
"repository": "[email protected]:AnandChowdhary/staart.git",
"author": "Anand Chowdhary <[email protected]>",
Expand Down Expand Up @@ -147,5 +147,5 @@
"setup"
],
"snyk": true,
"staart-version": "1.1.11"
"staart-version": "1.1.12"
}
4 changes: 2 additions & 2 deletions src/controllers/v1/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from "../../helpers/middleware";
import { CREATED } from "http-status-codes";
import asyncHandler from "express-async-handler";
import { safeRedirect, joiValidate } from "../../helpers/utils";
import { safeRedirect, joiValidate, hashIdToId } from "../../helpers/utils";
import Joi from "@hapi/joi";
import { FRONTEND_URL, BASE_URL } from "../../config";
import {
Expand Down Expand Up @@ -223,7 +223,7 @@ export class AuthController {
)
async getImpersonate(req: Request, res: Response) {
const tokenUserId = res.locals.token.id;
const impersonateUserId = parseInt(req.params.id);
const impersonateUserId = hashIdToId(req.params.id);
res.json(await impersonate(tokenUserId, impersonateUserId, res.locals));
}

Expand Down
7 changes: 4 additions & 3 deletions src/controllers/v1/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { authHandler, validator } from "../../helpers/middleware";
import asyncHandler from "express-async-handler";
import Joi from "@hapi/joi";
import { hashIdToId } from "../../helpers/utils";

@Controller("v1/memberships")
@ClassWrapper(asyncHandler)
Expand All @@ -25,7 +26,7 @@ export class MembershipController {
@Get(":id")
@Middleware(validator({ id: Joi.string().required() }, "params"))
async get(req: Request, res: Response) {
const membershipId = parseInt(req.params.id);
const membershipId = hashIdToId(req.params.id);
const userId = res.locals.token.id;
res.json(await getMembershipDetailsForUser(userId, membershipId));
}
Expand All @@ -34,7 +35,7 @@ export class MembershipController {
@Middleware(validator({ id: Joi.string().required() }, "params"))
async delete(req: Request, res: Response) {
const userId = res.locals.token.id;
const membershipId = parseInt(req.params.id);
const membershipId = hashIdToId(req.params.id);
await deleteMembershipForUser(userId, membershipId, res.locals);
res.json({ deleted: true });
}
Expand All @@ -43,7 +44,7 @@ export class MembershipController {
@Middleware(validator({ id: Joi.string().required() }, "params"))
async patch(req: Request, res: Response) {
const userId = res.locals.token.id;
const membershipId = parseInt(req.params.id);
const membershipId = hashIdToId(req.params.id);
const data = req.body;
delete req.body.id;
await updateMembershipForUser(userId, membershipId, data, res.locals);
Expand Down
31 changes: 16 additions & 15 deletions src/controllers/v1/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ import asyncHandler from "express-async-handler";
import {
joiValidate,
organizationUsernameToId,
localsToTokenOrKey
localsToTokenOrKey,
hashIdToId
} from "../../helpers/utils";
import Joi from "@hapi/joi";

Expand Down Expand Up @@ -513,7 +514,7 @@ export class OrganizationController {
@Get(":id/memberships/:membershipId")
async getMembership(req: Request, res: Response) {
const organizationId = await organizationUsernameToId(req.params.id);
const membershipId = parseInt(req.params.membershipId);
const membershipId = hashIdToId(req.params.membershipId);
joiValidate(
{
organizationId: Joi.string().required(),
Expand Down Expand Up @@ -543,7 +544,7 @@ export class OrganizationController {
)
async updateMembership(req: Request, res: Response) {
const organizationId = await organizationUsernameToId(req.params.id);
const membershipId = parseInt(req.params.membershipId);
const membershipId = hashIdToId(req.params.membershipId);
joiValidate(
{
organizationId: Joi.string().required(),
Expand All @@ -564,7 +565,7 @@ export class OrganizationController {
@Delete(":id/memberships/:membershipId")
async deleteMembership(req: Request, res: Response) {
const organizationId = await organizationUsernameToId(req.params.id);
const membershipId = parseInt(req.params.membershipId);
const membershipId = hashIdToId(req.params.membershipId);
joiValidate(
{
organizationId: Joi.string().required(),
Expand Down Expand Up @@ -639,7 +640,7 @@ export class OrganizationController {
@Get(":id/api-keys/:apiKeyId")
async getUserApiKey(req: Request, res: Response) {
const id = await organizationUsernameToId(req.params.id);
const apiKeyId = parseInt(req.params.apiKeyId);
const apiKeyId = hashIdToId(req.params.apiKeyId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand Down Expand Up @@ -667,7 +668,7 @@ export class OrganizationController {
)
async patchUserApiKey(req: Request, res: Response) {
const id = await organizationUsernameToId(req.params.id);
const apiKeyId = parseInt(req.params.apiKeyId);
const apiKeyId = hashIdToId(req.params.apiKeyId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand All @@ -689,7 +690,7 @@ export class OrganizationController {
@Delete(":id/api-keys/:apiKeyId")
async deleteUserApiKey(req: Request, res: Response) {
const id = await organizationUsernameToId(req.params.id);
const apiKeyId = parseInt(req.params.apiKeyId);
const apiKeyId = hashIdToId(req.params.apiKeyId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand All @@ -710,7 +711,7 @@ export class OrganizationController {
@Get(":id/api-keys/:apiKeyId/logs")
async getUserApiKeyLogs(req: Request, res: Response) {
const id = await organizationUsernameToId(req.params.id);
const apiKeyId = parseInt(req.params.apiKeyId);
const apiKeyId = hashIdToId(req.params.apiKeyId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand Down Expand Up @@ -782,7 +783,7 @@ export class OrganizationController {
@Get(":id/domains/:domainId")
async getUserDomain(req: Request, res: Response) {
const id = await organizationUsernameToId(req.params.id);
const domainId = parseInt(req.params.domainId);
const domainId = hashIdToId(req.params.domainId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand All @@ -806,7 +807,7 @@ export class OrganizationController {
)
async patchUserDomain(req: Request, res: Response) {
const id = await organizationUsernameToId(req.params.id);
const domainId = parseInt(req.params.domainId);
const domainId = hashIdToId(req.params.domainId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand All @@ -828,7 +829,7 @@ export class OrganizationController {
@Delete(":id/domains/:domainId")
async deleteUserDomain(req: Request, res: Response) {
const id = await organizationUsernameToId(req.params.id);
const domainId = parseInt(req.params.domainId);
const domainId = hashIdToId(req.params.domainId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand All @@ -849,7 +850,7 @@ export class OrganizationController {
@Post(":id/domains/:domainId/verify")
async verifyOrganizationDomain(req: Request, res: Response) {
const id = await organizationUsernameToId(req.params.id);
const domainId = parseInt(req.params.domainId);
const domainId = hashIdToId(req.params.domainId);
const method = req.body.method || req.query.method;
joiValidate(
{
Expand Down Expand Up @@ -928,7 +929,7 @@ export class OrganizationController {
@Get(":id/webhooks/:webhookId")
async getUserWebhook(req: Request, res: Response) {
const id = await organizationUsernameToId(req.params.id);
const webhookId = parseInt(req.params.webhookId);
const webhookId = hashIdToId(req.params.webhookId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand Down Expand Up @@ -960,7 +961,7 @@ export class OrganizationController {
)
async patchUserWebhook(req: Request, res: Response) {
const id = await organizationUsernameToId(req.params.id);
const webhookId = parseInt(req.params.webhookId);
const webhookId = hashIdToId(req.params.webhookId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand All @@ -982,7 +983,7 @@ export class OrganizationController {
@Delete(":id/webhooks/:webhookId")
async deleteUserWebhook(req: Request, res: Response) {
const id = await organizationUsernameToId(req.params.id);
const webhookId = parseInt(req.params.webhookId);
const webhookId = hashIdToId(req.params.webhookId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand Down
24 changes: 12 additions & 12 deletions src/controllers/v1/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
} from "../../rest/email";
import { CREATED } from "http-status-codes";
import asyncHandler from "express-async-handler";
import { joiValidate, userUsernameToId } from "../../helpers/utils";
import { joiValidate, userUsernameToId, hashIdToId } from "../../helpers/utils";
import Joi from "@hapi/joi";
import {
deleteMembershipForUser,
Expand Down Expand Up @@ -167,7 +167,7 @@ export class UserController {
@Get(":id/memberships/:membershipId")
async getMembership(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const membershipId = parseInt(req.params.membershipId);
const membershipId = hashIdToId(req.params.membershipId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand All @@ -181,7 +181,7 @@ export class UserController {
@Delete(":id/memberships/:membershipId")
async deleteMembership(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const membershipId = parseInt(req.params.membershipId);
const membershipId = hashIdToId(req.params.membershipId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand All @@ -196,7 +196,7 @@ export class UserController {
@Patch(":id/memberships/:membershipId")
async updateMembership(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const membershipId = parseInt(req.params.membershipId);
const membershipId = hashIdToId(req.params.membershipId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand Down Expand Up @@ -250,7 +250,7 @@ export class UserController {
@Get(":id/emails/:emailId")
async getEmail(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const emailId = parseInt(req.params.emailId);
const emailId = hashIdToId(req.params.emailId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand All @@ -264,7 +264,7 @@ export class UserController {
@Post(":id/emails/:emailId/resend")
async postResend(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const emailId = parseInt(req.params.emailId);
const emailId = hashIdToId(req.params.emailId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand All @@ -279,7 +279,7 @@ export class UserController {
@Delete(":id/emails/:emailId")
async deleteEmail(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const emailId = parseInt(req.params.emailId);
const emailId = hashIdToId(req.params.emailId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand Down Expand Up @@ -408,7 +408,7 @@ export class UserController {
@Get(":id/access-tokens/:accessTokenId")
async getUserAccessToken(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const accessTokenId = parseInt(req.params.accessTokenId);
const accessTokenId = hashIdToId(req.params.accessTokenId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand All @@ -434,7 +434,7 @@ export class UserController {
)
async patchUserAccessToken(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const accessTokenId = parseInt(req.params.accessTokenId);
const accessTokenId = hashIdToId(req.params.accessTokenId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand All @@ -456,7 +456,7 @@ export class UserController {
@Delete(":id/access-tokens/:accessTokenId")
async deleteUserAccessToken(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const accessTokenId = parseInt(req.params.accessTokenId);
const accessTokenId = hashIdToId(req.params.accessTokenId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand Down Expand Up @@ -497,7 +497,7 @@ export class UserController {
@Get(":id/sessions/:sessionId")
async getUserSession(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const sessionId = parseInt(req.params.sessionId);
const sessionId = hashIdToId(req.params.sessionId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand All @@ -511,7 +511,7 @@ export class UserController {
@Delete(":id/sessions/:sessionId")
async deleteUserSession(req: Request, res: Response) {
const id = await userUsernameToId(req.params.id, res.locals.token.id);
const sessionId = parseInt(req.params.sessionId);
const sessionId = hashIdToId(req.params.sessionId);
joiValidate(
{
id: [Joi.string().required(), Joi.string().required()],
Expand Down
2 changes: 1 addition & 1 deletion src/crud/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export const getVerifiedEmailObject = async (email: string) => {
* Get a list of all verified emails of a user
*/
export const getUserVerifiedEmails = async (user: User | string) => {
let userId = 0;
let userId = "";
if (typeof user === "object" && user.id) {
userId = user.id;
} else if (typeof user === "string") {
Expand Down
6 changes: 3 additions & 3 deletions src/crud/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export const createBackupCodes = async (userId: string, count = 1) => {
/**
* Update a backup code
*/
export const updateBackupCode = async (backupCode: string, code: KeyValue) => {
export const updateBackupCode = async (backupCode: number, code: KeyValue) => {
code.updatedAt = new Date();
return await query(
`UPDATE \`backup-codes\` SET ${setValues(code)} WHERE code = ?`,
Expand All @@ -282,7 +282,7 @@ export const updateBackupCode = async (backupCode: string, code: KeyValue) => {
/**
* Delete a backup code
*/
export const deleteBackupCode = async (backupCode: string) => {
export const deleteBackupCode = async (backupCode: number) => {
return await query("DELETE FROM `backup-codes` WHERE code = ?", [backupCode]);
};

Expand All @@ -303,7 +303,7 @@ export const getUserBackupCodes = async (userId: string) => {
/**
* Get a specific backup code
*/
export const getUserBackupCode = async (userId: string, backupCode: string) => {
export const getUserBackupCode = async (userId: string, backupCode: number) => {
return (<BackupCode[]>(
await query(
"SELECT * FROM `backup-codes` WHERE userId = ? AND code = ? LIMIT 1",
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test("Convert date to MySQL datetime", () => {
test("Remove sensitive info", () => {
expect(
deleteSensitiveInfoUser({
id: 1,
id: "wiuhoeijpaoe",
name: "Anand Chowdhary",
password: "1abc9c"
}).password
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
dateValues,
readOnlyValues,
generateHashId,
hashIdToId
hashIdToId,
IdValues
} from "./utils";
import { getUserPrimaryEmailObject } from "../crud/email";
import { InsertResult } from "../interfaces/mysql";
Expand Down Expand Up @@ -93,7 +94,7 @@ export const uncleanValues = (
)
).toISOString();
}
if (key === "id") item[key] = generateHashId(item[key]);
if (IdValues.includes(key)) item[key] = generateHashId(item[key]);
if (typeof item[key] === "string") item[key] = emojify(item[key]);
});
return item;
Expand Down
Loading

0 comments on commit 63de116

Please sign in to comment.