Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 101 additions & 28 deletions api-sample/database/database-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,121 @@ USE `{{PROJECT_NAME_SNAKECASE}}_db`;
-- DONT MODIFY THIS MIGRATION
-- it is used by Xest local development pipeline
DROP TABLE IF EXISTS `migrations`;
CREATE TABLE `migrations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`run_on` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_unicode_ci;

INSERT INTO `migrations` (
name,
run_on
) VALUES (
"/20211107064324-database-schema",
"20211107064324"
);

CREATE TABLE
`migrations` (
`id` int (11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`run_on` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE = InnoDB AUTO_INCREMENT = 0 DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_unicode_ci;

INSERT INTO
`migrations` (name, run_on)
VALUES
(
"/20211107064324-database-schema",
"20211107064324"
);

-- YOU CAN MODIFY BELOW THIS LINE
DROP TABLE IF EXISTS user_types;
CREATE TABLE user_types(
user_type_id int AUTO_INCREMENT PRIMARY KEY,
user_type VARCHAR(50) NOT NULL

-- USER_ORGANIZATION_ROLES TABLE START
DROP TABLE IF EXISTS user_organization_roles;
CREATE TABLE user_organization_roles(
user_organization_role_id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
user_organization_role VARCHAR(100) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_unicode_ci;
-- USER_ROLES TABLE END

-- USERS TABLE START
DROP TABLE IF EXISTS users;
CREATE TABLE users(
user_id int AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(500) NOT NULL,
user_type_id int NOT NULL,
CREATE TABLE
users (
user_id int AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(500) NOT NULL,
is_super_admin tinyint NOT NULL,
updated_at DATETIME DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_unicode_ci;
-- USERS TABLE END

-- ORGANIZATIONS TABLE START
DROP TABLE IF EXISTS organizations;
CREATE TABLE
organizations (
organization_id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
organization_name VARCHAR(100) NOT NULL,
updated_at DATETIME DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_unicode_ci;
-- ORGANIZATIONS TABLE END

-- USER_ORGANIZATIONS TABLE START
DROP TABLE IF EXISTS user_organizations;
CREATE TABLE user_organizations(
user_organization_id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
user_id INT NOT NULL,
user_organization_role_id INT NOT NULL,
organization_id INT NOT NULL,
added_by INT NOT NULL,
updated_at DATETIME DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_type_id) REFERENCES user_types(user_type_id)
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (user_organization_role_id) REFERENCES user_organization_roles(user_organization_role_id),
FOREIGN KEY (organization_id) REFERENCES organizations(organization_id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_unicode_ci;
-- USER_ORGANIZATIONS TABLE END

-- USER_ORGANIZATION_INVITATIONS TABLE START
DROP TABLE IF EXISTS user_organization_invitations;
CREATE TABLE user_organization_invitations(
user_organization_invitation_id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
invitation_shortcode VARCHAR(36) NOT NULL,
organization_id INT NOT NULL,
email VARCHAR (200) NOT NULL,
invited_by INT NOT NULL,
user_organization_role_id INT NOT NULL,
comment VARCHAR(500),
sent_at DATETIME DEFAULT CURRENT_TIMESTAMP,
accepted_at DATETIME,
declined_at DATETIME,
updated_at DATETIME DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (user_organization_role_id) REFERENCES user_organization_roles(user_organization_role_id),
FOREIGN KEY (organization_id) REFERENCES organizations(organization_id),
FOREIGN KEY (invited_by) REFERENCES user_organizations(user_id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_unicode_ci;
-- USER_ORGANIZATION_INVITATIONS TABLE END

-- USER_ORGANIZATION_REQUESTS TABLE START
DROP TABLE IF EXISTS password_recovery_requests;
CREATE TABLE password_recovery_requests(
password_recovery_request_id int AUTO_INCREMENT PRIMARY KEY,
requested_email VARCHAR(150) NOT NULL,
shortcode VARCHAR(40) NOT NULL UNIQUE,
shortcode VARCHAR(36) NOT NULL UNIQUE,
recovered_at DATETIME,
expiry_date DATETIME NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (requested_email) REFERENCES users(email)
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;
-- USER_ORGANIZATION_REQUESTS TABLE END

-- USER_ORGANIZATION_REQUESTS TABLE START
DROP TABLE IF EXISTS registration_requests;
CREATE TABLE registration_requests(
registration_request_id int AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(500) NOT NULL,
is_super_admin int NOT NULL,
organization_name VARCHAR(50) NOT NULL,
registration_shortcode VARCHAR(36) NOT NULL UNIQUE,
status VARCHAR(20) DEFAULT 'pending',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE utf8mb4_unicode_ci;
-- USER_ORGANIZATION_REQUESTS TABLE END
194 changes: 174 additions & 20 deletions api-sample/database/seed-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,180 @@ USE `{{PROJECT_NAME_SNAKECASE}}_db`;

-- DONT MODIFY THIS MIGRATION
-- it is used by Xest local development pipeline
INSERT INTO `migrations` (
name,
run_on
INSERT INTO
`migrations` (name, run_on)
VALUES
("/20211107064324-seed-data", "20211107064324");

-- YOU CAN MODIFY BELOW THIS LINE
INSERT INTO
user_organization_roles (user_organization_role_id, user_organization_role)
VALUES
(1, "OrganizationAdmin"),
(2, "Member");

INSERT INTO
users (
user_id,
first_name,
last_name,
email,
password,
is_super_admin,
created_at
)
VALUES
(
1,
"Ahmet",
"Akinsql",
"ahmet@akinsql.com",
SHA2 (CONCAT ("12345678", "SECRET_SALT"), 224),
1,
"2020-11-20 12:00:00"
),
(
2,
"Joe",
"Bloggs",
"joebloggs@gmail.com",
SHA2 (CONCAT ("12345678", "SECRET_SALT"), 224),
0,
"2020-11-20 12:00:00"
),
(
3,
"Jim",
"Bloggs",
"jimbloggs@yahoo.com",
SHA2 (CONCAT ("12345678", "SECRET_SALT"), 224),
1,
"2020-11-20 12:00:00"
),
(
4,
"Jane",
"Doe",
"janedoe@example.com",
SHA2 (CONCAT ("12345678", "SECRET_SALT"), 224),
0,
"2020-11-20 12:00:00"
);

INSERT INTO
organizations (organization_name)
VALUES
('Organization 1'),
('Organization 2'),
('Organization 3'),
('Organization 4'),
('Organization 5');

INSERT INTO
user_organizations (
user_id,
user_organization_role_id,
organization_id,
added_by
)
VALUES
(1, 1, 1, 1),
(2, 2, 2, 2),
(3, 1, 1, 3),
(4, 2, 2, 4);

INSERT INTO
user_organization_invitations (
invitation_shortcode,
organization_id,
email,
invited_by,
user_organization_role_id,
comment,
sent_at,
accepted_at
)
VALUES
(
'shortcode1',
1,
'email1@example.com',
1,
1,
'Comment 1',
'2022-01-01 00:00:00',
'2022-01-02 00:00:00'
),
(
'shortcode2',
1,
'email2@example.com',
1,
2,
'Comment 2',
'2022-01-02 00:00:00',
'2022-01-03 00:00:00'
),
(
'shortcode3',
2,
'email3@example.com',
2,
1,
'Comment 3',
'2022-01-03 00:00:00',
'2022-01-04 00:00:00'
),
(
'shortcode4',
2,
'email4@example.com',
2,
2,
'Comment 4',
'2022-01-04 00:00:00',
'2022-01-05 00:00:00'
);

-- INSERT INTO password_recovery_requests(shortcode,requested_email,expiry_date,created_at)
-- VALUES ("321","ahmet@akinsql.com","2020-09-20 12:30:00","2022-01-03 12:30:00");

INSERT INTO password_recovery_requests(
requested_email,
shortcode,
expiry_date
) VALUES (
"/20211107064324-seed-data",
"20211107064324"
'ahmet@akinsql.com',
'shortcode1',
DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 1 DAY)
), (
'joebloggs@gmail.com',
'shortcode2',
DATE_ADD(CURRENT_TIMESTAMP, INTERVAL 1 DAY)
);

-- YOU CAN MODIFY BELOW THIS LINE
INSERT INTO user_types (user_type_id, user_type)
VALUES (1, "admin");
INSERT INTO user_types (user_type_id, user_type)
VALUES (2, "user");

INSERT INTO users (user_id, first_name, last_name, email, password, user_type_id, created_at)
VALUES (1, "Ahmet", "Akinsql", "ahmet@akinsql.com", SHA2(CONCAT("password","SECRET_SALT"), 224), 1, "2020-11-20 12:00:00");
INSERT INTO users (user_id, first_name, last_name, email, password, user_type_id, created_at)
VALUES (2, "Joe", "Bloggs","joebloggs@gmail.com", SHA2(CONCAT("password","SECRET_SALT"), 224), 2, "2020-11-20 12:00:00");
INSERT INTO users (user_id, first_name, last_name, email, password, user_type_id, created_at)
VALUES (3, "Jim", "Bloggs" , "jimbloggs@yahoo.com", SHA2(CONCAT("password","SECRET_SALT"), 224), 2, "2020-11-20 12:00:00");

INSERT INTO password_recovery_requests(shortcode,requested_email,expiry_date,created_at)
VALUES ("321","ahmet@akinsql.com","2020-09-20 12:30:00","2022-01-03 12:30:00");
INSERT INTO registration_requests(
first_name,
last_name,
email,
password,
is_super_admin,
organization_name,
registration_shortcode
) VALUES (
'John',
'Doe',
'john.doe@example.com',
SHA2 (CONCAT ("12345678", "SECRET_SALT"), 224),
1,
'Example Organization',
'shortcode1'
), (
'Jane',
'Doe',
'joebloggs@gmail.com',
SHA2 (CONCAT ("12345678", "SECRET_SALT"), 224),
0,
'Example Organization 2',
'shortcode2'
);
3 changes: 2 additions & 1 deletion api-sample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
"jsonwebtoken": "^8.5.1",
"jwks-rsa": "^1.5.0",
"lodash": "^4.17.19",
"mailgun.js": "^10.1.0",
"minio": "^7.0.19",
"mjml": "^4.5.1",
"module-alias": "^2.2.0",
Expand All @@ -147,6 +148,6 @@
"winston-transport": "^4.3.0",
"yamlify-object": "^0.5.1",
"yamlify-object-colors": "^1.0.3",
"yup": "^0.27.0"
"yup": "^1.3.2"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const insertOrganizationInvitation = require("./queries/insertOrganizationInvitation");

const createOrganizationInvitation = async ({
userId,
orgId,
email,
userOrganizationRoleId,
comment,
invitationShortcode
}) => {
const newInvitationId = await insertOrganizationInvitation({
userId,
orgId,
email,
userOrganizationRoleId,
comment,
invitationShortcode
});

return { newInvitationId };
};

module.exports = createOrganizationInvitation;
Loading