Skip to content

Commit 142715e

Browse files
committed
feat: use crud.getHost and crud.getorganization
1 parent 4773bb8 commit 142715e

File tree

1 file changed

+12
-49
lines changed

1 file changed

+12
-49
lines changed

src/server.js

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ const vm = require('vm');
44
const Config = require("@cocreate/config");
55
const { URL } = require('url');
66

7-
const organizations = {};
8-
const hosts = {};
7+
// const organizations = {};
98

109
class CoCreateLazyLoader {
1110
constructor(server, crud, files) {
@@ -56,28 +55,9 @@ class CoCreateLazyLoader {
5655
const valideUrl = new URL(`http://${req.headers.host}${req.url}`);
5756
const hostname = valideUrl.hostname;
5857

59-
let organization = hosts[hostname];
60-
if (!organization) {
61-
let org = await this.crud.send({
62-
method: 'object.read',
63-
array: 'organizations',
64-
$filter: {
65-
query: [
66-
{ key: "host", value: [hostname], operator: "$in" }
67-
]
68-
},
69-
organization_id: process.env.organization_id
70-
})
71-
72-
if (!org || !org.object || !org.object[0]) {
73-
return this.files.send(req, res, this.crud, organization, valideUrl)
74-
} else {
75-
organization = org.object[0]
76-
organizations[organization._id] = organization
77-
}
78-
}
79-
80-
hosts[hostname] = organization
58+
let organization = await this.crud.getHost(hostname);
59+
if (organization.error)
60+
return this.files.send(req, res, this.crud, organization, valideUrl)
8161

8262
if (valideUrl.pathname.startsWith('/webhooks/')) {
8363
let name = req.url.split('/')[2]; // Assuming URL structure is /webhook/name/...
@@ -160,31 +140,14 @@ class CoCreateLazyLoader {
160140
}
161141

162142
async getApiKey(organization_id, name) {
163-
organizations[organization_id] = this.getOrganization(organization_id, name)
164-
organizations[organization_id] = await organizations[organization_id]
165-
return organizations[organization_id][name]
166-
}
167-
168-
async getOrganization(organization_id) {
169-
let organization = await this.crud.send({
170-
method: 'object.read',
171-
database: organization_id,
172-
array: 'organizations',
173-
object: [{ _id: organization_id }],
174-
organization_id
175-
})
176-
177-
if (organization
178-
&& organization.object
179-
&& organization.object[0]) {
180-
if (organization.object[0].apis) {
181-
return organization.object[0].apis
182-
} else
183-
return { error: 'No apis defined could not be found' }
184-
} else {
185-
return { serverOrganization: false, error: 'An organization could not be found' }
186-
}
187-
143+
let organization = await this.crud.getOrganization(organization_id, false);
144+
if (!organization.error)
145+
return organization.error
146+
else if (!organization.apis)
147+
return { error: 'Missing apis object in organization object' }
148+
if (!organization.apis[name])
149+
return { error: `Missing ${name} in organization apis object` }
150+
return organization.apis[name]
188151
}
189152

190153
}

0 commit comments

Comments
 (0)