Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit f238ae6

Browse files
✨ Support for API key in org
1 parent ff68114 commit f238ae6

File tree

4 files changed

+73
-124
lines changed

4 files changed

+73
-124
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "staart",
3-
"version": "1.0.31",
3+
"version": "1.0.32",
44
"main": "index.js",
55
"repository": "git@github.com:AnandChowdhary/staart.git",
66
"author": "Anand Chowdhary <mail@anandchowdhary.com>",

src/controllers/organization.ts

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class OrganizationController {
9494
const id = await organizationUsernameToId(req.params.id);
9595
joiValidate({ id: Joi.number().required() }, { id });
9696
await updateOrganizationForUser(
97-
res.locals.token.id,
97+
localsToTokenOrKey(res),
9898
id,
9999
req.body,
100100
res.locals
@@ -105,12 +105,15 @@ export class OrganizationController {
105105
@Delete(":id")
106106
async delete(req: Request, res: Response) {
107107
const organizationId = await organizationUsernameToId(req.params.id);
108-
const userId = res.locals.token.id;
109108
joiValidate(
110109
{ organizationId: Joi.number().required() },
111110
{ organizationId }
112111
);
113-
await deleteOrganizationForUser(userId, organizationId, res.locals);
112+
await deleteOrganizationForUser(
113+
res.locals.token.id,
114+
organizationId,
115+
res.locals
116+
);
114117
res.json({ success: true, message: "organization-deleted" });
115118
}
116119

@@ -122,7 +125,10 @@ export class OrganizationController {
122125
{ organizationId }
123126
);
124127
res.json(
125-
await getOrganizationBillingForUser(res.locals.token.id, organizationId)
128+
await getOrganizationBillingForUser(
129+
localsToTokenOrKey(res),
130+
organizationId
131+
)
126132
);
127133
}
128134

@@ -134,7 +140,7 @@ export class OrganizationController {
134140
{ organizationId }
135141
);
136142
await updateOrganizationBillingForUser(
137-
res.locals.token.id,
143+
localsToTokenOrKey(res),
138144
organizationId,
139145
req.body,
140146
res.locals
@@ -162,7 +168,7 @@ export class OrganizationController {
162168
);
163169
res.json(
164170
await getOrganizationInvoicesForUser(
165-
res.locals.token.id,
171+
localsToTokenOrKey(res),
166172
organizationId,
167173
subscriptionParams
168174
)
@@ -182,7 +188,7 @@ export class OrganizationController {
182188
);
183189
res.json(
184190
await getOrganizationInvoiceForUser(
185-
res.locals.token.id,
191+
localsToTokenOrKey(res),
186192
organizationId,
187193
invoiceId
188194
)
@@ -206,7 +212,7 @@ export class OrganizationController {
206212
);
207213
res.json(
208214
await getOrganizationSourcesForUser(
209-
res.locals.token.id,
215+
localsToTokenOrKey(res),
210216
organizationId,
211217
subscriptionParams
212218
)
@@ -226,7 +232,7 @@ export class OrganizationController {
226232
);
227233
res.json(
228234
await getOrganizationSourceForUser(
229-
res.locals.token.id,
235+
localsToTokenOrKey(res),
230236
organizationId,
231237
sourceId
232238
)
@@ -253,7 +259,7 @@ export class OrganizationController {
253259
);
254260
res.json(
255261
await getOrganizationSubscriptionsForUser(
256-
res.locals.token.id,
262+
localsToTokenOrKey(res),
257263
organizationId,
258264
subscriptionParams
259265
)
@@ -273,7 +279,7 @@ export class OrganizationController {
273279
);
274280
res.json(
275281
await getOrganizationSubscriptionForUser(
276-
res.locals.token.id,
282+
localsToTokenOrKey(res),
277283
organizationId,
278284
subscriptionId
279285
)
@@ -303,7 +309,7 @@ export class OrganizationController {
303309
);
304310
res.json(
305311
await updateOrganizationSubscriptionForUser(
306-
res.locals.token.id,
312+
localsToTokenOrKey(res),
307313
organizationId,
308314
subscriptionId,
309315
data
@@ -330,7 +336,7 @@ export class OrganizationController {
330336
);
331337
res.json(
332338
await createOrganizationSubscriptionForUser(
333-
res.locals.token.id,
339+
localsToTokenOrKey(res),
334340
organizationId,
335341
subscriptionParams
336342
)
@@ -350,7 +356,7 @@ export class OrganizationController {
350356
);
351357
res.json(
352358
await getOrganizationPricingPlansForUser(
353-
res.locals.token.id,
359+
localsToTokenOrKey(res),
354360
organizationId,
355361
product
356362
)
@@ -368,7 +374,7 @@ export class OrganizationController {
368374
.status(CREATED)
369375
.json(
370376
await createOrganizationSourceForUser(
371-
res.locals.token.id,
377+
localsToTokenOrKey(res),
372378
organizationId,
373379
req.body
374380
)
@@ -388,7 +394,7 @@ export class OrganizationController {
388394
);
389395
res.json(
390396
await deleteOrganizationSourceForUser(
391-
res.locals.token.id,
397+
localsToTokenOrKey(res),
392398
organizationId,
393399
sourceId
394400
)
@@ -408,7 +414,7 @@ export class OrganizationController {
408414
);
409415
res.json(
410416
await updateOrganizationSourceForUser(
411-
res.locals.token.id,
417+
localsToTokenOrKey(res),
412418
organizationId,
413419
sourceId,
414420
req.body
@@ -424,7 +430,10 @@ export class OrganizationController {
424430
{ organizationId }
425431
);
426432
res.json(
427-
await getAllOrganizationDataForUser(res.locals.token.id, organizationId)
433+
await getAllOrganizationDataForUser(
434+
localsToTokenOrKey(res),
435+
organizationId
436+
)
428437
);
429438
}
430439

@@ -437,7 +446,7 @@ export class OrganizationController {
437446
);
438447
res.json(
439448
await getOrganizationRecentEventsForUser(
440-
res.locals.token.id,
449+
localsToTokenOrKey(res),
441450
organizationId
442451
)
443452
);
@@ -452,7 +461,7 @@ export class OrganizationController {
452461
);
453462
res.json(
454463
await getOrganizationMembershipsForUser(
455-
res.locals.token.id,
464+
localsToTokenOrKey(res),
456465
organizationId,
457466
req.query
458467
)
@@ -484,7 +493,7 @@ export class OrganizationController {
484493
}
485494
);
486495
await inviteMemberToOrganization(
487-
res.locals.token.id,
496+
localsToTokenOrKey(res),
488497
organizationId,
489498
newMemberName,
490499
newMemberEmail,
@@ -510,7 +519,11 @@ export class OrganizationController {
510519
apiKeyParams
511520
);
512521
res.json(
513-
await getOrganizationApiKeysForUser(res.locals.token.id, id, apiKeyParams)
522+
await getOrganizationApiKeysForUser(
523+
localsToTokenOrKey(res),
524+
id,
525+
apiKeyParams
526+
)
514527
);
515528
}
516529

@@ -525,7 +538,12 @@ export class OrganizationController {
525538
res
526539
.status(CREATED)
527540
.json(
528-
await createApiKeyForUser(res.locals.token.id, id, access, res.locals)
541+
await createApiKeyForUser(
542+
localsToTokenOrKey(res),
543+
id,
544+
access,
545+
res.locals
546+
)
529547
);
530548
}
531549

@@ -541,7 +559,7 @@ export class OrganizationController {
541559
{ id, apiKey }
542560
);
543561
res.json(
544-
await getOrganizationApiKeyForUser(res.locals.token.id, id, apiKey)
562+
await getOrganizationApiKeyForUser(localsToTokenOrKey(res), id, apiKey)
545563
);
546564
}
547565

@@ -558,7 +576,7 @@ export class OrganizationController {
558576
);
559577
res.json(
560578
await updateApiKeyForUser(
561-
res.locals.token.id,
579+
localsToTokenOrKey(res),
562580
id,
563581
apiKey,
564582
req.body,
@@ -579,7 +597,7 @@ export class OrganizationController {
579597
{ id, apiKey }
580598
);
581599
res.json(
582-
await deleteApiKeyForUser(res.locals.token.id, id, apiKey, res.locals)
600+
await deleteApiKeyForUser(localsToTokenOrKey(res), id, apiKey, res.locals)
583601
);
584602
}
585603
}

src/rest/membership.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
getMembershipDetailed,
1616
updateMembership
1717
} from "../crud/membership";
18-
import { User } from "../interfaces/tables/user";
18+
import { User, ApiKey } from "../interfaces/tables/user";
1919
import { register } from "./auth";
2020
import { can } from "../helpers/authorization";
2121
import { Locals, KeyValue } from "../interfaces/general";
@@ -33,7 +33,7 @@ export const getMembershipDetailsForUser = async (
3333
};
3434

3535
export const inviteMemberToOrganization = async (
36-
userId: number,
36+
userId: number | ApiKey,
3737
organizationId: number,
3838
newMemberName: string,
3939
newMemberEmail: string,
@@ -80,7 +80,7 @@ export const inviteMemberToOrganization = async (
8080
};
8181

8282
export const deleteMembershipForUser = async (
83-
tokenUserId: number,
83+
tokenUserId: number | ApiKey,
8484
membershipId: number,
8585
locals: Locals
8686
) => {
@@ -99,22 +99,13 @@ export const deleteMembershipForUser = async (
9999
throw new Error(ErrorCode.CANNOT_DELETE_SOLE_OWNER);
100100
}
101101
await deleteMembership(membership.id);
102-
await createEvent(
103-
{
104-
userId: tokenUserId,
105-
organizationId: membership.organizationId,
106-
type: EventType.MEMBERSHIP_DELETED,
107-
data: { membership }
108-
},
109-
locals
110-
);
111102
return;
112103
}
113104
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
114105
};
115106

116107
export const updateMembershipForUser = async (
117-
userId: number,
108+
userId: number | ApiKey,
118109
membershipId: number,
119110
data: KeyValue,
120111
locals: Locals
@@ -134,15 +125,6 @@ export const updateMembershipForUser = async (
134125
}
135126
}
136127
await updateMembership(membershipId, data);
137-
await createEvent(
138-
{
139-
userId,
140-
organizationId: membership.organizationId,
141-
type: EventType.MEMBERSHIP_UPDATED,
142-
data: { membership: data }
143-
},
144-
locals
145-
);
146128
return;
147129
}
148130
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);

0 commit comments

Comments
 (0)