@m5kdev/backend 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +18 -0
- package/dist/src/lib/posthog.js +7 -0
- package/dist/src/lib/sentry.js +9 -0
- package/dist/src/modules/access/access.repository.js +32 -0
- package/dist/src/modules/access/access.service.js +51 -0
- package/dist/src/modules/access/access.test.js +182 -0
- package/dist/src/modules/access/access.utils.js +20 -0
- package/dist/src/modules/ai/ai.db.js +39 -0
- package/dist/src/modules/ai/ai.prompt.js +30 -0
- package/dist/src/modules/ai/ai.repository.js +26 -0
- package/dist/src/modules/ai/ai.router.js +132 -0
- package/dist/src/modules/ai/ai.service.js +207 -0
- package/dist/src/modules/ai/ai.trpc.d.ts +5 -5
- package/dist/src/modules/ai/ai.trpc.js +20 -0
- package/dist/src/modules/ai/ideogram/ideogram.constants.js +167 -0
- package/dist/src/modules/ai/ideogram/ideogram.dto.js +49 -0
- package/dist/src/modules/ai/ideogram/ideogram.prompt.js +860 -0
- package/dist/src/modules/ai/ideogram/ideogram.repository.js +46 -0
- package/dist/src/modules/ai/ideogram/ideogram.service.js +11 -0
- package/dist/src/modules/auth/auth.db.js +215 -0
- package/dist/src/modules/auth/auth.dto.js +38 -0
- package/dist/src/modules/auth/auth.lib.d.ts +4 -4
- package/dist/src/modules/auth/auth.lib.js +284 -0
- package/dist/src/modules/auth/auth.middleware.js +52 -0
- package/dist/src/modules/auth/auth.repository.js +541 -0
- package/dist/src/modules/auth/auth.service.js +201 -0
- package/dist/src/modules/auth/auth.trpc.d.ts +18 -18
- package/dist/src/modules/auth/auth.trpc.js +157 -0
- package/dist/src/modules/auth/auth.utils.js +97 -0
- package/dist/src/modules/base/base.abstract.js +53 -0
- package/dist/src/modules/base/base.dto.js +112 -0
- package/dist/src/modules/base/base.grants.js +123 -0
- package/dist/src/modules/base/base.grants.test.js +668 -0
- package/dist/src/modules/base/base.repository.js +307 -0
- package/dist/src/modules/base/base.service.js +109 -0
- package/dist/src/modules/base/base.types.js +2 -0
- package/dist/src/modules/billing/billing.db.js +29 -0
- package/dist/src/modules/billing/billing.repository.js +235 -0
- package/dist/src/modules/billing/billing.router.js +56 -0
- package/dist/src/modules/billing/billing.service.js +147 -0
- package/dist/src/modules/billing/billing.trpc.d.ts +5 -5
- package/dist/src/modules/billing/billing.trpc.js +17 -0
- package/dist/src/modules/clay/clay.repository.js +26 -0
- package/dist/src/modules/clay/clay.service.js +24 -0
- package/dist/src/modules/connect/connect.db.js +30 -0
- package/dist/src/modules/connect/connect.dto.js +36 -0
- package/dist/src/modules/connect/connect.linkedin.js +53 -0
- package/dist/src/modules/connect/connect.oauth.js +198 -0
- package/dist/src/modules/connect/connect.repository.d.ts +7 -7
- package/dist/src/modules/connect/connect.repository.js +54 -0
- package/dist/src/modules/connect/connect.router.js +54 -0
- package/dist/src/modules/connect/connect.service.d.ts +14 -14
- package/dist/src/modules/connect/connect.service.js +114 -0
- package/dist/src/modules/connect/connect.trpc.d.ts +10 -10
- package/dist/src/modules/connect/connect.trpc.js +21 -0
- package/dist/src/modules/connect/connect.types.js +2 -0
- package/dist/src/modules/crypto/crypto.db.js +17 -0
- package/dist/src/modules/crypto/crypto.repository.js +10 -0
- package/dist/src/modules/crypto/crypto.service.js +52 -0
- package/dist/src/modules/email/email.service.js +107 -0
- package/dist/src/modules/file/file.repository.js +79 -0
- package/dist/src/modules/file/file.router.js +99 -0
- package/dist/src/modules/file/file.service.js +150 -0
- package/dist/src/modules/recurrence/recurrence.db.js +66 -0
- package/dist/src/modules/recurrence/recurrence.repository.js +39 -0
- package/dist/src/modules/recurrence/recurrence.service.js +70 -0
- package/dist/src/modules/recurrence/recurrence.trpc.d.ts +15 -15
- package/dist/src/modules/recurrence/recurrence.trpc.js +65 -0
- package/dist/src/modules/social/social.dto.js +18 -0
- package/dist/src/modules/social/social.linkedin.js +427 -0
- package/dist/src/modules/social/social.linkedin.test.js +235 -0
- package/dist/src/modules/social/social.service.js +76 -0
- package/dist/src/modules/social/social.types.js +2 -0
- package/dist/src/modules/tag/tag.db.js +42 -0
- package/dist/src/modules/tag/tag.dto.js +9 -0
- package/dist/src/modules/tag/tag.repository.js +154 -0
- package/dist/src/modules/tag/tag.service.js +31 -0
- package/dist/src/modules/tag/tag.trpc.d.ts +5 -5
- package/dist/src/modules/tag/tag.trpc.js +47 -0
- package/dist/src/modules/utils/applyPagination.js +16 -0
- package/dist/src/modules/utils/applySorting.js +18 -0
- package/dist/src/modules/utils/getConditionsFromFilters.js +200 -0
- package/dist/src/modules/video/video.service.js +84 -0
- package/dist/src/modules/webhook/webhook.constants.js +10 -0
- package/dist/src/modules/webhook/webhook.db.js +17 -0
- package/dist/src/modules/webhook/webhook.dto.js +7 -0
- package/dist/src/modules/webhook/webhook.repository.js +56 -0
- package/dist/src/modules/webhook/webhook.router.js +30 -0
- package/dist/src/modules/webhook/webhook.service.js +68 -0
- package/dist/src/modules/workflow/workflow.db.js +30 -0
- package/dist/src/modules/workflow/workflow.repository.js +105 -0
- package/dist/src/modules/workflow/workflow.service.js +37 -0
- package/dist/src/modules/workflow/workflow.trpc.d.ts +5 -5
- package/dist/src/modules/workflow/workflow.trpc.js +21 -0
- package/dist/src/modules/workflow/workflow.types.js +2 -0
- package/dist/src/modules/workflow/workflow.utils.js +173 -0
- package/dist/src/test/stubs/utils.js +5 -0
- package/dist/src/trpc/context.d.ts +5 -5
- package/dist/src/trpc/context.js +17 -0
- package/dist/src/trpc/index.js +6 -0
- package/dist/src/trpc/procedures.d.ts +56 -56
- package/dist/src/trpc/procedures.js +32 -0
- package/dist/src/trpc/utils.js +20 -0
- package/dist/src/types.d.ts +33 -33
- package/dist/src/types.js +13 -0
- package/dist/src/utils/errors.js +104 -0
- package/dist/src/utils/logger.js +11 -0
- package/dist/src/utils/posthog.js +31 -0
- package/dist/src/utils/types.js +2 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/tsconfig.json +2 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deleteManyOutput = exports.deleteOutput = exports.scheduleManyOutput = exports.scheduleOutput = exports.uuidManyOutput = exports.uuidOutput = exports.getTableColumns = exports.createTableSelectSchema = void 0;
|
|
4
|
+
exports.toZodFilter = toZodFilter;
|
|
5
|
+
exports.pickSchema = pickSchema;
|
|
6
|
+
exports.pickTableSchema = pickTableSchema;
|
|
7
|
+
exports.omitSchema = omitSchema;
|
|
8
|
+
exports.omitTableSchema = omitTableSchema;
|
|
9
|
+
exports.pickColumns = pickColumns;
|
|
10
|
+
exports.pickTableColumns = pickTableColumns;
|
|
11
|
+
exports.omitTableColumns = omitTableColumns;
|
|
12
|
+
exports.createSelectDTO = createSelectDTO;
|
|
13
|
+
exports.createSelectUtils = createSelectUtils;
|
|
14
|
+
const drizzle_orm_1 = require("drizzle-orm");
|
|
15
|
+
Object.defineProperty(exports, "getTableColumns", { enumerable: true, get: function () { return drizzle_orm_1.getTableColumns; } });
|
|
16
|
+
const drizzle_zod_1 = require("drizzle-zod");
|
|
17
|
+
const zod_1 = require("zod");
|
|
18
|
+
// Wrapper to force the drizzle-zod overload to the Table version
|
|
19
|
+
const createTableSelectSchema = (table) => (0, drizzle_zod_1.createSelectSchema)(table);
|
|
20
|
+
exports.createTableSelectSchema = createTableSelectSchema;
|
|
21
|
+
function toZodFilter(array) {
|
|
22
|
+
return array.reduce((acc, column) => {
|
|
23
|
+
acc[column] = true;
|
|
24
|
+
return acc;
|
|
25
|
+
}, {});
|
|
26
|
+
}
|
|
27
|
+
function pickSchema(schema, keys) {
|
|
28
|
+
const mask = {};
|
|
29
|
+
for (const k of keys) {
|
|
30
|
+
mask[k] = true;
|
|
31
|
+
}
|
|
32
|
+
return schema.pick(mask);
|
|
33
|
+
}
|
|
34
|
+
function pickTableSchema(table, columns) {
|
|
35
|
+
return pickSchema((0, drizzle_zod_1.createSelectSchema)(table), columns);
|
|
36
|
+
}
|
|
37
|
+
function omitSchema(schema, keys) {
|
|
38
|
+
const mask = {};
|
|
39
|
+
for (const k of keys) {
|
|
40
|
+
mask[k] = true;
|
|
41
|
+
}
|
|
42
|
+
return schema.omit(mask);
|
|
43
|
+
}
|
|
44
|
+
function omitTableSchema(table, columns) {
|
|
45
|
+
return omitSchema((0, drizzle_zod_1.createSelectSchema)(table), columns);
|
|
46
|
+
}
|
|
47
|
+
function pickColumns(table, schema) {
|
|
48
|
+
const allColumns = (0, drizzle_orm_1.getTableColumns)(table);
|
|
49
|
+
const schemaKeys = Object.keys(schema.shape);
|
|
50
|
+
const result = {};
|
|
51
|
+
for (const key of schemaKeys) {
|
|
52
|
+
if (key in allColumns) {
|
|
53
|
+
result[key] = allColumns[key];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
function pickTableColumns(table, columns) {
|
|
59
|
+
const allColumns = (0, drizzle_orm_1.getTableColumns)(table);
|
|
60
|
+
const result = {};
|
|
61
|
+
for (const key of columns) {
|
|
62
|
+
if (key in allColumns) {
|
|
63
|
+
result[key] = allColumns[key];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
function omitTableColumns(table, columns) {
|
|
69
|
+
const allColumns = (0, drizzle_orm_1.getTableColumns)(table);
|
|
70
|
+
const columnsToOmit = new Set(columns);
|
|
71
|
+
const filteredEntries = Object.entries(allColumns).filter(([key]) => !columnsToOmit.has(key));
|
|
72
|
+
return Object.fromEntries(filteredEntries);
|
|
73
|
+
}
|
|
74
|
+
// Implementation
|
|
75
|
+
function createSelectDTO(table, partial) {
|
|
76
|
+
if (partial) {
|
|
77
|
+
if ("omit" in partial && partial.omit) {
|
|
78
|
+
return {
|
|
79
|
+
columns: omitTableColumns(table, partial.omit),
|
|
80
|
+
schema: omitTableSchema(table, partial.omit),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
if ("pick" in partial && partial.pick) {
|
|
84
|
+
return {
|
|
85
|
+
columns: pickTableColumns(table, partial.pick),
|
|
86
|
+
schema: pickTableSchema(table, partial.pick),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return { columns: (0, drizzle_orm_1.getTableColumns)(table), schema: (0, exports.createTableSelectSchema)(table) };
|
|
91
|
+
}
|
|
92
|
+
function createSelectUtils(dtos, output, transformer) {
|
|
93
|
+
return {
|
|
94
|
+
select: Object.fromEntries(Object.entries(dtos).map(([key, dto]) => [key, dto.columns])),
|
|
95
|
+
output,
|
|
96
|
+
transformer,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
exports.uuidOutput = zod_1.z.object({
|
|
100
|
+
id: zod_1.z.uuid(),
|
|
101
|
+
});
|
|
102
|
+
exports.uuidManyOutput = zod_1.z.object({
|
|
103
|
+
ids: zod_1.z.array(zod_1.z.uuid()),
|
|
104
|
+
});
|
|
105
|
+
exports.scheduleOutput = zod_1.z.object({
|
|
106
|
+
jobId: zod_1.z.string(),
|
|
107
|
+
});
|
|
108
|
+
exports.scheduleManyOutput = zod_1.z.object({
|
|
109
|
+
jobIds: zod_1.z.array(zod_1.z.string()),
|
|
110
|
+
});
|
|
111
|
+
exports.deleteOutput = exports.uuidOutput;
|
|
112
|
+
exports.deleteManyOutput = exports.uuidManyOutput;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.flattenNestedGrants = flattenNestedGrants;
|
|
4
|
+
exports.checkPermissionSync = checkPermissionSync;
|
|
5
|
+
exports.checkPermissionAsync = checkPermissionAsync;
|
|
6
|
+
const neverthrow_1 = require("neverthrow");
|
|
7
|
+
function flattenNestedGrants(nestedGrants) {
|
|
8
|
+
return Object.entries(nestedGrants).flatMap(([resource, levels]) => {
|
|
9
|
+
return Object.entries(levels).flatMap(([level, roles]) => {
|
|
10
|
+
return Object.entries(roles).flatMap(([role, actions]) => {
|
|
11
|
+
return Object.entries(actions).map(([action, access]) => {
|
|
12
|
+
return {
|
|
13
|
+
resource,
|
|
14
|
+
level: level,
|
|
15
|
+
role,
|
|
16
|
+
action,
|
|
17
|
+
access,
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function checkOwnership(entityField, contextValue, entities) {
|
|
25
|
+
if (!contextValue)
|
|
26
|
+
return false;
|
|
27
|
+
if (!entities)
|
|
28
|
+
return false;
|
|
29
|
+
return Array.isArray(entities)
|
|
30
|
+
? entities.every((e) => e[entityField] === contextValue)
|
|
31
|
+
: entities[entityField] === contextValue;
|
|
32
|
+
}
|
|
33
|
+
// Level priority: user -> team -> organization (bottom-up)
|
|
34
|
+
const LEVEL_PRIORITY = ["user", "team", "organization"];
|
|
35
|
+
function getRoleForLevel(level, ctx) {
|
|
36
|
+
switch (level) {
|
|
37
|
+
case "user":
|
|
38
|
+
return ctx.userRole;
|
|
39
|
+
case "team":
|
|
40
|
+
return ctx.teamRole;
|
|
41
|
+
case "organization":
|
|
42
|
+
return ctx.organizationRole;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function getContextValueForLevel(level, ctx) {
|
|
46
|
+
switch (level) {
|
|
47
|
+
case "user":
|
|
48
|
+
return ctx.userId;
|
|
49
|
+
case "team":
|
|
50
|
+
return ctx.teamId;
|
|
51
|
+
case "organization":
|
|
52
|
+
return ctx.organizationId;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function getOwnershipFieldForLevel(level) {
|
|
56
|
+
switch (level) {
|
|
57
|
+
case "user":
|
|
58
|
+
return "userId";
|
|
59
|
+
case "team":
|
|
60
|
+
return "teamId";
|
|
61
|
+
case "organization":
|
|
62
|
+
return "organizationId";
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function hasAllAccess(grants, roles) {
|
|
66
|
+
for (const level of LEVEL_PRIORITY) {
|
|
67
|
+
for (const grant of grants) {
|
|
68
|
+
if (grant.level !== level)
|
|
69
|
+
continue;
|
|
70
|
+
if (grant.access !== "all")
|
|
71
|
+
continue;
|
|
72
|
+
if (grant.role === getRoleForLevel(level, roles))
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
function checkOwnAccess(grants, roles, contextValues, entities) {
|
|
79
|
+
for (const level of LEVEL_PRIORITY) {
|
|
80
|
+
for (const grant of grants) {
|
|
81
|
+
if (grant.level !== level)
|
|
82
|
+
continue;
|
|
83
|
+
if (grant.access !== "own")
|
|
84
|
+
continue;
|
|
85
|
+
if (grant.role !== getRoleForLevel(level, roles))
|
|
86
|
+
continue;
|
|
87
|
+
const ownershipField = getOwnershipFieldForLevel(level);
|
|
88
|
+
const contextValue = getContextValueForLevel(level, contextValues);
|
|
89
|
+
if (checkOwnership(ownershipField, contextValue, entities))
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
function checkPermissionSync(ctx, grants, entities) {
|
|
96
|
+
if (!grants || grants.length === 0)
|
|
97
|
+
return false;
|
|
98
|
+
const { id: userId, role: userRole } = ctx.user;
|
|
99
|
+
const { activeOrganizationRole: organizationRole, activeTeamRole: teamRole, activeOrganizationId: organizationId, activeTeamId: teamId, } = ctx.session;
|
|
100
|
+
const roles = { userRole, teamRole, organizationRole };
|
|
101
|
+
const contextValues = { userId, teamId, organizationId };
|
|
102
|
+
// Pass 1: Check for "all" access first (no ownership check needed)
|
|
103
|
+
if (hasAllAccess(grants, roles))
|
|
104
|
+
return true;
|
|
105
|
+
// Pass 2: Check "own" access with ownership validation
|
|
106
|
+
return checkOwnAccess(grants, roles, contextValues, entities);
|
|
107
|
+
}
|
|
108
|
+
async function checkPermissionAsync(ctx, grants, getEntities) {
|
|
109
|
+
if (!grants || grants.length === 0)
|
|
110
|
+
return (0, neverthrow_1.ok)(false);
|
|
111
|
+
const { id: userId, role: userRole } = ctx.user;
|
|
112
|
+
const { activeOrganizationRole: organizationRole, activeTeamRole: teamRole, activeOrganizationId: organizationId, activeTeamId: teamId, } = ctx.session;
|
|
113
|
+
const roles = { userRole, teamRole, organizationRole };
|
|
114
|
+
const contextValues = { userId, teamId, organizationId };
|
|
115
|
+
// Pass 1: Check for "all" access first (no entity fetch needed)
|
|
116
|
+
if (hasAllAccess(grants, roles))
|
|
117
|
+
return (0, neverthrow_1.ok)(true);
|
|
118
|
+
// Pass 2: Only fetch entities if we need to check ownership
|
|
119
|
+
const entities = await getEntities();
|
|
120
|
+
if (entities.isErr())
|
|
121
|
+
return (0, neverthrow_1.err)(entities.error);
|
|
122
|
+
return (0, neverthrow_1.ok)(checkOwnAccess(grants, roles, contextValues, entities.value));
|
|
123
|
+
}
|