-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathprofileProxyConfigs.ts
More file actions
103 lines (95 loc) · 3.17 KB
/
Copy pathprofileProxyConfigs.ts
File metadata and controls
103 lines (95 loc) · 3.17 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*********************************************************************
* Copyright (c) Intel Corporation 2025
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
import { type IProfileProxyConfigsTable } from '../../../interfaces/database/IProfileProxyConfigsDb.js'
import { type ProfileProxyConfigs } from '../../../models/RCS.Config.js'
import { API_UNEXPECTED_EXCEPTION } from '../../../utils/constants.js'
import { RPSError } from '../../../utils/RPSError.js'
import format from 'pg-format'
import type PostgresDb from '../index.js'
import { PostgresErr } from '../errors.js'
export class ProfileProxyConfigsTable implements IProfileProxyConfigsTable {
db: PostgresDb
constructor(db: PostgresDb) {
this.db = db
}
/**
* @description Get AMT Profile associated proxy configs
* @param {string} profileName
* @returns {ProfileProxyConfigs[]} Return an array of proxy configs
*/
async getProfileProxyConfigs(profileName: string, tenantId = ''): Promise<ProfileProxyConfigs[]> {
const results = await this.db.query<ProfileProxyConfigs>(
`
SELECT
priority as "priority",
proxy_config_name as "name"
FROM profiles_proxyconfigs
WHERE profile_name = $1 and tenant_id = $2
ORDER BY priority`,
[profileName, tenantId]
)
return results.rows
}
/**
* @description Insert proxy configs associated with AMT profile
* @param {ProfileProxyConfigs[]} proxyConfigs
* @param {string} profileName
* @returns {ProfileProxyConfigs[]} Return an array of proxy configs
*/
async createProfileProxyConfigs(
proxyConfigs: ProfileProxyConfigs[],
profileName: string,
tenantId = ''
): Promise<boolean> {
try {
if (proxyConfigs.length < 1) {
throw new RPSError('No proxyConfigs provided to insert')
}
// Preparing data for inserting multiple rows
const configs = proxyConfigs.map((config) => [
config.name,
profileName,
config.priority,
tenantId
])
const proxyProfilesQueryResults = await this.db.query(
format(
`
INSERT INTO
profiles_proxyconfigs (proxy_config_name, profile_name, priority, tenant_id)
VALUES %L`,
configs
)
)
if ((proxyProfilesQueryResults?.rowCount ?? 0) > 0) {
return true
}
} catch (error) {
if (error.code === PostgresErr.C23_FOREIGN_KEY_VIOLATION) {
throw new RPSError(error.detail, 'Foreign key constraint violation')
}
throw new RPSError(API_UNEXPECTED_EXCEPTION(profileName))
}
return false
}
/**
* @description Delete proxy configs of an AMT Profile from DB by profile name
* @param {string} configName
* @returns {boolean} Return true on successful deletion
*/
async deleteProfileProxyConfigs(configName: string, tenantId = ''): Promise<boolean> {
const deleteProfileWifiResults = await this.db.query(
`
DELETE
FROM profiles_proxyconfigs
WHERE profile_name = $1 and tenant_id = $2`,
[configName, tenantId]
)
if ((deleteProfileWifiResults?.rowCount ?? 0) > 0) {
return true
}
return false
}
}