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

Commit 390768a

Browse files
✨ Add support for creating emails
1 parent 2bb3045 commit 390768a

File tree

7 files changed

+155
-13
lines changed

7 files changed

+155
-13
lines changed

schema.sql

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
SET NAMES utf8mb4;
2+
SET FOREIGN_KEY_CHECKS = 0;
3+
4+
-- ----------------------------
5+
-- Table structure for backup-codes
6+
-- ----------------------------
7+
DROP TABLE IF EXISTS `backup-codes`;
8+
CREATE TABLE `backup-codes` (
9+
`code` int(6) NOT NULL,
10+
`userId` int(11) NOT NULL,
11+
`used` int(1) NOT NULL DEFAULT '0',
12+
`createdAt` datetime NOT NULL,
13+
`updatedAt` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP,
14+
PRIMARY KEY (`code`)
15+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
16+
17+
-- ----------------------------
18+
-- Table structure for emails
19+
-- ----------------------------
20+
DROP TABLE IF EXISTS `emails`;
21+
CREATE TABLE `emails` (
22+
`id` int(11) NOT NULL AUTO_INCREMENT,
23+
`email` varchar(255) NOT NULL,
24+
`userId` int(11) NOT NULL,
25+
`isVerified` int(1) NOT NULL DEFAULT '0',
26+
`isPrimary` int(1) NOT NULL DEFAULT '0',
27+
`createdAt` datetime NOT NULL,
28+
`updatedAt` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP,
29+
PRIMARY KEY (`id`)
30+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
31+
32+
-- ----------------------------
33+
-- Table structure for memberships
34+
-- ----------------------------
35+
DROP TABLE IF EXISTS `memberships`;
36+
CREATE TABLE `memberships` (
37+
`id` int(11) NOT NULL AUTO_INCREMENT,
38+
`organizationId` int(11) NOT NULL,
39+
`userId` int(11) NOT NULL,
40+
`role` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'member',
41+
`createdAt` datetime NOT NULL,
42+
`updatedAt` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP,
43+
PRIMARY KEY (`id`)
44+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
45+
46+
-- ----------------------------
47+
-- Table structure for organizations
48+
-- ----------------------------
49+
DROP TABLE IF EXISTS `organizations`;
50+
CREATE TABLE `organizations` (
51+
`id` int(11) NOT NULL AUTO_INCREMENT,
52+
`name` varchar(255) DEFAULT NULL,
53+
`invitationDomain` varchar(255) DEFAULT NULL,
54+
`createdAt` datetime NOT NULL,
55+
`updatedAt` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP,
56+
PRIMARY KEY (`id`)
57+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
58+
59+
-- ----------------------------
60+
-- Table structure for users
61+
-- ----------------------------
62+
DROP TABLE IF EXISTS `users`;
63+
CREATE TABLE `users` (
64+
`id` int(12) NOT NULL AUTO_INCREMENT,
65+
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
66+
`nickname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
67+
`primaryEmail` int(12) DEFAULT NULL,
68+
`password` varchar(255) DEFAULT NULL,
69+
`twoFactorEnabled` int(1) NOT NULL DEFAULT '0',
70+
`twoFactorSecret` varchar(255) DEFAULT NULL,
71+
`countryCode` varchar(2) DEFAULT NULL,
72+
`timezone` varchar(255) NOT NULL DEFAULT 'Europe/Amsterdam',
73+
`notificationEmails` int(1) NOT NULL DEFAULT '1',
74+
`preferredLanguage` varchar(5) NOT NULL DEFAULT 'en-us',
75+
`prefersReducedMotion` int(1) NOT NULL DEFAULT '0',
76+
`createdAt` datetime(6) NOT NULL,
77+
`updatedAt` datetime(6) NOT NULL ON UPDATE CURRENT_TIMESTAMP(6),
78+
PRIMARY KEY (`id`)
79+
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
80+
81+
SET FOREIGN_KEY_CHECKS = 1;

src/crud/email.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { query, tableValues } from "../helpers/mysql";
2+
import { Email } from "../interfaces/tables/emails";
3+
4+
export const createEmail = async (email: Email) => {
5+
// Clean up values
6+
email.email = email.email.toLowerCase();
7+
email.isVerified = false;
8+
email.isPrimary = false;
9+
email.createdAt = new Date();
10+
email.updatedAt = email.createdAt;
11+
// Create user
12+
return await query(
13+
`INSERT INTO emails ${tableValues(email)}`,
14+
Object.values(email)
15+
);
16+
};

src/crud/user.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { query, tableValues } from "../helpers/mysql";
1+
import { query, tableValues, setValues } from "../helpers/mysql";
22
import { User } from "../interfaces/tables/user";
33
import { capitalizeFirstAndLastLetter } from "../helpers/string";
44
import { hash } from "bcrypt";
@@ -26,3 +26,15 @@ export const createUser = async (user: User) => {
2626
Object.values(user)
2727
);
2828
};
29+
30+
interface KV {
31+
[index: string]: any;
32+
}
33+
export const updateUser = async (id: number, user: KV) => {
34+
user.updatedAt = user.createdAt;
35+
// Create user
36+
return await query(`UPDATE users SET ${setValues(user)} WHERE id = ?`, [
37+
...Object.values(user),
38+
id
39+
]);
40+
};

src/helpers/mysql.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export const cleanValues = (
5050
values: (string | number | boolean | Date | undefined)[]
5151
) => {
5252
values = values.map(value => {
53+
// Clean up strings
54+
if (typeof value === "string") value = value.trim();
5355
// Convert true to 1, false to 0
5456
if (typeof value === "boolean") value = !!value;
5557
// Convert Date to mysql datetime
@@ -62,3 +64,14 @@ export const cleanValues = (
6264
});
6365
return values;
6466
};
67+
68+
interface KV {
69+
[index: string]: any;
70+
}
71+
export const setValues = (object: KV) => {
72+
let query = "";
73+
Object.keys(object).forEach(key => {
74+
query += `${key} = "${object[key]}", `;
75+
});
76+
return query.slice(0, -2);
77+
};

src/interfaces/mysql.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface InsertResult {
2+
fieldCount: number;
3+
affectedRows: number;
4+
insertId: number;
5+
serverStatus: number;
6+
warningCount: number;
7+
message: string;
8+
protocol41: boolean;
9+
changedRows: number;
10+
}

src/interfaces/tables/emails.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export interface Email {
2-
id: number;
2+
id?: number;
33
email: string;
44
userId: number;
5-
isVerified: boolean;
6-
isPrimary: boolean;
7-
createdAt: Date;
8-
updatedAt: Date;
5+
isVerified?: boolean;
6+
isPrimary?: boolean;
7+
createdAt?: Date;
8+
updatedAt?: Date;
99
}

src/rest/auth.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import { User } from "../interfaces/tables/user";
2-
import { createUser } from "../crud/user";
2+
import { createUser, updateUser } from "../crud/user";
3+
import { InsertResult } from "../interfaces/mysql";
4+
import { createEmail } from "../crud/email";
35

46
export const register = async (
5-
user?: User,
7+
user: User,
68
email?: string,
79
organizationId?: string
810
) => {
9-
const result = await createUser({
10-
name: "Anand Chowdhary"
11-
});
12-
console.log(result);
13-
return "done!";
11+
// Create user
12+
const result = <InsertResult>await createUser(user);
13+
const userId = result.insertId;
14+
// Set email
15+
if (email) {
16+
const newEmail = <InsertResult>await createEmail({
17+
userId,
18+
email,
19+
isPrimary: true
20+
});
21+
const emailId = newEmail.insertId;
22+
await updateUser(userId, { primaryEmail: emailId });
23+
}
1424
};

0 commit comments

Comments
 (0)