-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstant.js
More file actions
82 lines (61 loc) · 2.02 KB
/
constant.js
File metadata and controls
82 lines (61 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const sql = require('mssql');
// Db creds
const config = {
user: 'sa',
password: 'admin',
server: 'BHOOMI',
database: 'sample',
options: {
encrypt: true,
trustServerCertificate: true ,
instancename:"SQLEXPRESS" },
port:1433
};
// a function to check the tenant id
const tenantCheck = async (tenant) => {
console.log(tenant)
console.log("Tenant check", config);
let pool;
let transaction;
try {
pool = new sql.ConnectionPool(config);
await pool.connect();
transaction = new sql.Transaction(pool);
await transaction.begin();
const request = new sql.Request(transaction);
const checkQuery = `select * from Tenants where tenantID= @tenant `;
request.input('tenant', sql.Int, tenant);
const result = await request.query(checkQuery);
const recordset = result.recordset;
if (recordset.length === 0) {
const insertQuery = `INSERT INTO Tenants (tenantID, storageLimitMB)
VALUES (@tenant, @storageLimit)`;
request.input('storageLimit', sql.Int, 500);
await request.query(insertQuery);
}
await transaction.commit();
console.log('Tenant checked/inserted successfully');
} catch (err) {
console.log(err);
if (transaction) await transaction.rollback();
if (err.number === 2627 || err.number === 2601) {
return {
status: 409,
error: "Duplicate tenantName Error",
message: "The tenantName already exists in the database."
};
}
console.error('Error during tenant transaction: ', err);
return {
status: 500,
message: 'Server error'
};
} finally {
if (pool) {
await pool.close();
}
}
};
module.exports={
config,tenantCheck
}