Skip to content

Commit 4c27829

Browse files
CubeRomanMagellanMagellan
andauthored
SK-446: Organisations support (#71)
* add organisationId to config * add organizationId to all http request * add orgId to create user --------- Co-authored-by: Magellan <magellan@connectycube.com>
1 parent 3cd1fcc commit 4c27829

4 files changed

Lines changed: 23 additions & 15 deletions

File tree

lib/src/api/connection/http_request.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Future<Map<String, dynamic>> sendHTTPRequest(
1313
String requestName, dynamic requestData,
1414
[Map? requestHeaders]) async {
1515
final url = 'https://${await SecureStorage.instance.getEnvironmentUrl()}';
16+
final orgId = await SecureStorage.instance.getEnvironmentOrgId();
17+
requestData['organization_id'] = orgId;
1618
var urlQuery = buildQueryUrl(url, [requestName]);
1719
var body = jsonEncode(requestData);
1820
Map<String, String> headers = Map.of(_headers);

lib/src/api/users/users_api.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ Future<User> createUser({
3030
String? lastName,
3131
String? email,
3232
String? phone,
33-
}) {
33+
}) async {
3434
return SamaConnectionService.instance.sendRequest(userCreateRequestName, {
3535
'login': login,
3636
'password': password,
3737
'device_id': deviceId,
38+
'organization_id': await SecureStorage.instance.getEnvironmentOrgId(),
3839
if (email != null) 'email': email,
3940
if (phone != null) 'phone': phone,
4041
if (firstName != null) 'first_name': firstName,

lib/src/api/utils/config.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
const String apiProdUrl = 'api.samacloud.io';
2-
const String apiDevUrl = 'api-dev.samacloud.io';
1+
const String _apiProdUrl = 'api.samacloud.io';
2+
const String _apiDevUrl = 'api-dev.samacloud.io';
3+
4+
const String _organizationIdProd = '6821d147b2bb04e5fe564c73';
5+
const String _organizationIdDev = '6821d147b2bb04e5fe564c73';
36

47
enum EnvType {
5-
dev(apiDevUrl),
6-
prod(apiProdUrl);
8+
prod(_apiProdUrl, _organizationIdProd),
9+
dev(_apiDevUrl, _organizationIdDev);
710

811
final String url;
12+
final String organizationId;
913

10-
const EnvType(this.url);
11-
12-
factory EnvType.fromUrl(String url) {
13-
return values.firstWhere((e) => e.url == url);
14-
}
14+
const EnvType(this.url, this.organizationId);
1515
}

lib/src/shared/secure_storage.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const String storageAccessToken = "storage_access_token";
1717
const String storageAccessTokenExpiration = "storage_access_token_expiration";
1818
const String storageRefreshToken = "storage_refresh_token";
1919
const String storageSubscriptionToken = "storage_subscription_token";
20-
const String storageEnvironmentUrl = "storage_environment_url";
20+
const String storageEnvironmentType = "storage_environment_type";
2121

2222
class SecureStorage {
2323
static final SecureStorage _instance = SecureStorage._internal();
@@ -110,7 +110,7 @@ class SecureStorage {
110110

111111
Future<void> deleteAllData() async {
112112
deleteCurrentUser();
113-
_storage.delete(key: storageEnvironmentUrl);
113+
_storage.delete(key: storageEnvironmentType);
114114
}
115115

116116
saveAccessToken(AccessToken token) {
@@ -143,15 +143,20 @@ class SecureStorage {
143143
}
144144

145145
saveEnvironmentType(EnvType type) {
146-
_storage.write(key: storageEnvironmentUrl, value: type.url);
146+
_storage.write(key: storageEnvironmentType, value: type.name);
147147
}
148148

149149
Future<EnvType> getDevEnvironmentType() async {
150-
return EnvType.fromUrl(await getEnvironmentUrl());
150+
return EnvType.values
151+
.byName(await _storage.read(key: storageEnvironmentType) ?? 'dev');
151152
}
152153

153154
Future<String> getEnvironmentUrl() async {
154-
return (await _storage.read(key: storageEnvironmentUrl)) ?? EnvType.dev.url;
155+
return (await getDevEnvironmentType()).url;
156+
}
157+
158+
Future<String> getEnvironmentOrgId() async {
159+
return (await getDevEnvironmentType()).organizationId;
155160
}
156161
}
157162

0 commit comments

Comments
 (0)