@appconda/nextjs 1.0.161 → 1.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.
Files changed (277) hide show
  1. package/dist/actions/actionClient.d.ts +8 -2
  2. package/dist/actions/actionClient.js +21 -15
  3. package/dist/actions/auth.d.ts +1 -0
  4. package/dist/actions/auth.js +7 -0
  5. package/dist/actions/authOptions.d.ts +5 -0
  6. package/dist/actions/authOptions.js +235 -0
  7. package/dist/actions/index.d.ts +3 -0
  8. package/dist/actions/index.js +4 -0
  9. package/dist/actions/nextAuthHandler.d.ts +1 -0
  10. package/dist/actions/nextAuthHandler.js +6 -0
  11. package/dist/client.d.ts +1 -1
  12. package/dist/client.js +1 -0
  13. package/dist/decorators/Cache.js +1 -0
  14. package/dist/decorators/CacheKey.js +1 -0
  15. package/dist/decorators/Invalidate.js +1 -0
  16. package/dist/enums/api-service.js +1 -0
  17. package/dist/enums/api.js +1 -0
  18. package/dist/enums/auth-method.js +1 -0
  19. package/dist/enums/browser.js +1 -0
  20. package/dist/enums/compression.js +1 -0
  21. package/dist/enums/credit-card.js +1 -0
  22. package/dist/enums/database-usage-range.js +1 -0
  23. package/dist/enums/email-template-locale.js +1 -0
  24. package/dist/enums/email-template-type.js +1 -0
  25. package/dist/enums/entities/EntityLimitType.js +1 -0
  26. package/dist/enums/entities/PropertyAttributeName.js +1 -0
  27. package/dist/enums/entities/PropertyCondition.js +1 -0
  28. package/dist/enums/entities/PropertyType.js +1 -0
  29. package/dist/enums/entities/PropertyValueType.js +1 -0
  30. package/dist/enums/entities/RowAccess.js +1 -0
  31. package/dist/enums/entities/ViewFilterCondition.js +1 -0
  32. package/dist/enums/execution-method.js +1 -0
  33. package/dist/enums/flag.js +1 -0
  34. package/dist/enums/function-usage-range.js +1 -0
  35. package/dist/enums/image-format.js +1 -0
  36. package/dist/enums/image-gravity.js +1 -0
  37. package/dist/enums/index-type.js +1 -0
  38. package/dist/enums/messaging-provider-type.js +1 -0
  39. package/dist/enums/name.js +1 -0
  40. package/dist/enums/o-auth-provider copy.js +1 -0
  41. package/dist/enums/password-hash.js +1 -0
  42. package/dist/enums/platform-type.js +1 -0
  43. package/dist/enums/project-usage-range.js +1 -0
  44. package/dist/enums/region.js +1 -0
  45. package/dist/enums/relation-mutate.js +1 -0
  46. package/dist/enums/relationship-type.js +1 -0
  47. package/dist/enums/resource-type.js +1 -0
  48. package/dist/enums/runtime.js +1 -0
  49. package/dist/enums/s-m-t-p-secure.js +1 -0
  50. package/dist/enums/shared/ApplicationLayout.js +1 -0
  51. package/dist/enums/shared/Colors.js +1 -0
  52. package/dist/enums/shared/InputType.js +1 -0
  53. package/dist/enums/shared/Periodicity.js +1 -0
  54. package/dist/enums/shared/SvgIcon.js +1 -0
  55. package/dist/enums/shared/Theme.js +1 -0
  56. package/dist/enums/sms-template-locale.js +1 -0
  57. package/dist/enums/sms-template-type.js +1 -0
  58. package/dist/enums/smtp-encryption.js +1 -0
  59. package/dist/enums/storage-usage-range.js +1 -0
  60. package/dist/enums/subscriptions/PricingModel.js +1 -0
  61. package/dist/enums/subscriptions/SubscriptionBillingPeriod.js +1 -0
  62. package/dist/enums/subscriptions/SubscriptionFeatureLimitType.js +1 -0
  63. package/dist/enums/subscriptions/SubscriptionPriceType.js +1 -0
  64. package/dist/enums/tenants/LinkedAccountStatus.js +1 -0
  65. package/dist/enums/tenants/TenantUserJoined.js +1 -0
  66. package/dist/enums/tenants/TenantUserStatus.js +1 -0
  67. package/dist/enums/tenants/TenantUserType.js +1 -0
  68. package/dist/enums/user-usage-range.js +1 -0
  69. package/dist/getAppcondaClient.js +5 -3
  70. package/dist/getSDKForCurrentUser.d.ts +8 -2
  71. package/dist/getSDKForCurrentUser.js +13 -5
  72. package/dist/getSDKForService.js +3 -1
  73. package/dist/getSDKForTenant.d.ts +20 -0
  74. package/dist/getSDKForTenant.js +44 -0
  75. package/dist/id.js +1 -0
  76. package/dist/index.d.ts +4 -1
  77. package/dist/index.js +5 -1
  78. package/dist/inputFile.js +1 -0
  79. package/dist/lib/Cache/Adapter.js +1 -0
  80. package/dist/lib/Cache/Adapters/Filesystem.js +1 -0
  81. package/dist/lib/Cache/Adapters/Memory.js +1 -0
  82. package/dist/lib/Cache/Adapters/None.js +1 -0
  83. package/dist/lib/Cache/Adapters/Sharding.js +1 -0
  84. package/dist/lib/Cache/Cache.js +1 -0
  85. package/dist/lib/Cache/index.js +1 -0
  86. package/dist/lib/Cache/test.js +1 -0
  87. package/dist/lib/Registry/Registry.js +1 -0
  88. package/dist/lib/Registry/index.js +1 -0
  89. package/dist/lib/Services.js +1 -0
  90. package/dist/lib/crypto.d.ts +23 -0
  91. package/dist/lib/crypto.js +78 -0
  92. package/dist/lib/env.d.ts +1 -0
  93. package/dist/lib/env.js +137 -0
  94. package/dist/lib/errors.d.ts +70 -0
  95. package/dist/lib/errors.js +76 -0
  96. package/dist/lib/index.d.ts +2 -0
  97. package/dist/lib/index.js +3 -0
  98. package/dist/lib/jwt.d.ts +12 -0
  99. package/dist/lib/jwt.js +103 -0
  100. package/dist/models.js +1 -0
  101. package/dist/modules/account/actions.d.ts +20 -14
  102. package/dist/modules/account/actions.js +1 -0
  103. package/dist/modules/account/enums/authentication-factor.js +1 -0
  104. package/dist/modules/account/enums/authenticator-type.js +1 -0
  105. package/dist/modules/account/enums/o-auth-provider.js +1 -0
  106. package/dist/modules/account/index.js +1 -0
  107. package/dist/modules/account/schema.d.ts +8 -8
  108. package/dist/modules/account/schema.js +1 -0
  109. package/dist/modules/account/service.js +1 -0
  110. package/dist/modules/account/types.js +1 -0
  111. package/dist/modules/acl/service.d.ts +26 -0
  112. package/dist/modules/acl/service.js +27 -0
  113. package/dist/modules/agent/action.d.ts +27 -23
  114. package/dist/modules/agent/action.js +1 -0
  115. package/dist/modules/agent/index.js +1 -0
  116. package/dist/modules/agent/schema.d.ts +18 -18
  117. package/dist/modules/agent/schema.js +1 -0
  118. package/dist/modules/agent/service.js +1 -0
  119. package/dist/modules/agent/types.js +1 -0
  120. package/dist/modules/ai/index.js +1 -0
  121. package/dist/modules/ai/node/actions.d.ts +1 -1
  122. package/dist/modules/ai/node/actions.js +1 -0
  123. package/dist/modules/ai/node/index.js +1 -0
  124. package/dist/modules/ai/node/service.js +1 -0
  125. package/dist/modules/hooks/index.d.ts +2 -0
  126. package/dist/modules/hooks/index.js +3 -0
  127. package/dist/modules/hooks/lib/Hooks.d.ts +32 -0
  128. package/dist/modules/hooks/lib/Hooks.js +64 -0
  129. package/dist/modules/hooks/lib/handler.d.ts +12 -0
  130. package/dist/modules/hooks/lib/handler.js +23 -0
  131. package/dist/modules/hooks/lib/jobs/index.d.ts +2 -0
  132. package/dist/modules/hooks/lib/jobs/index.js +3 -0
  133. package/dist/modules/hooks/lib/jobs/job1.d.ts +1 -0
  134. package/dist/modules/hooks/lib/jobs/job1.js +31 -0
  135. package/dist/modules/hooks/lib/jobs/log-job.d.ts +1 -0
  136. package/dist/modules/hooks/lib/jobs/log-job.js +9 -0
  137. package/dist/modules/hooks/lib/wait.d.ts +8 -0
  138. package/dist/modules/hooks/lib/wait.js +13 -0
  139. package/dist/modules/index.d.ts +4 -0
  140. package/dist/modules/index.js +5 -0
  141. package/dist/modules/scheduled-job/action.d.ts +97 -0
  142. package/dist/modules/scheduled-job/action.js +77 -0
  143. package/dist/modules/scheduled-job/index.d.ts +7 -0
  144. package/dist/modules/scheduled-job/index.js +8 -0
  145. package/dist/modules/scheduled-job/lib/Schedules.d.ts +32 -0
  146. package/dist/modules/scheduled-job/lib/Schedules.js +64 -0
  147. package/dist/modules/scheduled-job/lib/handler.d.ts +12 -0
  148. package/dist/modules/scheduled-job/lib/handler.js +23 -0
  149. package/dist/modules/scheduled-job/lib/jobs/index.d.ts +2 -0
  150. package/dist/modules/scheduled-job/lib/jobs/index.js +3 -0
  151. package/dist/modules/scheduled-job/lib/jobs/job1.d.ts +1 -0
  152. package/dist/modules/scheduled-job/lib/jobs/job1.js +30 -0
  153. package/dist/modules/scheduled-job/lib/jobs/log-job.d.ts +1 -0
  154. package/dist/modules/scheduled-job/lib/jobs/log-job.js +9 -0
  155. package/dist/modules/scheduled-job/lib/wait.d.ts +8 -0
  156. package/dist/modules/scheduled-job/lib/wait.js +13 -0
  157. package/dist/modules/scheduled-job/schema.d.ts +52 -0
  158. package/dist/modules/scheduled-job/schema.js +23 -0
  159. package/dist/modules/scheduled-job/service.d.ts +13 -0
  160. package/dist/modules/scheduled-job/service.js +25 -0
  161. package/dist/modules/scheduled-job/types.d.ts +29 -0
  162. package/dist/modules/scheduled-job/types.js +2 -0
  163. package/dist/modules/task/action.d.ts +201 -0
  164. package/dist/modules/task/action.js +147 -0
  165. package/dist/modules/task/index.d.ts +4 -0
  166. package/dist/modules/task/index.js +5 -0
  167. package/dist/modules/task/schema.d.ts +107 -0
  168. package/dist/modules/task/schema.js +44 -0
  169. package/dist/modules/task/service.d.ts +19 -0
  170. package/dist/modules/task/service.js +43 -0
  171. package/dist/modules/task/types.d.ts +84 -0
  172. package/dist/modules/task/types.js +2 -0
  173. package/dist/modules/tenant/actions.d.ts +49 -0
  174. package/dist/modules/tenant/actions.js +38 -0
  175. package/dist/modules/tenant/index.d.ts +3 -0
  176. package/dist/modules/tenant/index.js +4 -0
  177. package/dist/{services → modules/tenant}/tenant.d.ts +3 -3
  178. package/dist/modules/tenant/tenant.js +125 -0
  179. package/dist/modules/tenant/types.d.ts +11 -0
  180. package/dist/modules/tenant/types.js +2 -0
  181. package/dist/modules/waitlist/action.d.ts +31 -29
  182. package/dist/modules/waitlist/action.js +1 -0
  183. package/dist/modules/waitlist/index.js +1 -0
  184. package/dist/modules/waitlist/schema.d.ts +12 -12
  185. package/dist/modules/waitlist/schema.js +1 -0
  186. package/dist/modules/waitlist/service.js +1 -0
  187. package/dist/modules/waitlist/types.js +1 -0
  188. package/dist/permission.js +1 -0
  189. package/dist/query.js +1 -0
  190. package/dist/role.js +1 -0
  191. package/dist/schemas/nodes.js +1 -0
  192. package/dist/service-client.js +1 -0
  193. package/dist/service.js +1 -0
  194. package/dist/services/applets.js +1 -0
  195. package/dist/services/avatars.js +1 -0
  196. package/dist/services/chat-flow.js +1 -0
  197. package/dist/services/community.js +1 -0
  198. package/dist/services/configuration.js +1 -0
  199. package/dist/services/databases.js +1 -0
  200. package/dist/services/functions.js +1 -0
  201. package/dist/services/graphql.js +1 -0
  202. package/dist/services/health.js +1 -0
  203. package/dist/services/locale.js +1 -0
  204. package/dist/services/messaging.js +1 -0
  205. package/dist/services/permissions.js +1 -0
  206. package/dist/services/pricing.js +1 -0
  207. package/dist/services/projects.js +1 -0
  208. package/dist/services/roles.js +1 -0
  209. package/dist/services/schema.js +1 -0
  210. package/dist/services/storage.js +1 -0
  211. package/dist/services/subscription.js +1 -0
  212. package/dist/services/teams.js +1 -0
  213. package/dist/services/tenant-subscription.js +1 -0
  214. package/dist/services/users.js +1 -0
  215. package/package.json +4 -1
  216. package/src/actions/actionClient.ts +13 -6
  217. package/src/actions/auth.ts +7 -0
  218. package/src/actions/authOptions.ts +255 -0
  219. package/src/actions/index.ts +3 -0
  220. package/src/actions/nextAuthHandler.ts +9 -0
  221. package/src/client.ts +1 -1
  222. package/src/getAppcondaClient.ts +4 -3
  223. package/src/getSDKForCurrentUser.ts +12 -5
  224. package/src/getSDKForService.ts +2 -1
  225. package/src/getSDKForTenant.ts +52 -0
  226. package/src/index.ts +4 -2
  227. package/src/lib/crypto.ts +105 -0
  228. package/src/lib/env.ts +145 -0
  229. package/src/lib/errors.ts +138 -0
  230. package/src/lib/index.ts +2 -0
  231. package/src/lib/jwt.ts +113 -0
  232. package/src/modules/account/actions.ts +1 -1
  233. package/src/modules/acl/service.ts +55 -0
  234. package/src/modules/agent/action.ts +2 -0
  235. package/src/modules/ai/node/actions.ts +0 -1
  236. package/src/modules/hooks/index.ts +4 -0
  237. package/src/modules/hooks/lib/Hooks.ts +103 -0
  238. package/src/modules/hooks/lib/handler.ts +27 -0
  239. package/src/modules/hooks/lib/jobs/index.ts +2 -0
  240. package/src/modules/hooks/lib/jobs/job1.ts +35 -0
  241. package/src/modules/hooks/lib/jobs/log-job.ts +9 -0
  242. package/src/modules/hooks/lib/wait.ts +17 -0
  243. package/src/modules/index.ts +5 -1
  244. package/src/modules/scheduled-job/action.ts +94 -0
  245. package/src/modules/scheduled-job/index.ts +8 -0
  246. package/src/modules/scheduled-job/lib/Schedules.ts +103 -0
  247. package/src/modules/scheduled-job/lib/handler.ts +27 -0
  248. package/src/modules/scheduled-job/lib/jobs/index.ts +2 -0
  249. package/src/modules/scheduled-job/lib/jobs/job1.ts +34 -0
  250. package/src/modules/scheduled-job/lib/jobs/log-job.ts +9 -0
  251. package/src/modules/scheduled-job/lib/wait.ts +17 -0
  252. package/src/modules/scheduled-job/schema.ts +31 -0
  253. package/src/modules/scheduled-job/service.ts +37 -0
  254. package/src/modules/scheduled-job/types.ts +35 -0
  255. package/src/modules/task/action.ts +175 -0
  256. package/src/modules/task/index.ts +4 -0
  257. package/src/modules/task/schema.ts +53 -0
  258. package/src/modules/task/service.ts +62 -0
  259. package/src/modules/task/types.ts +89 -0
  260. package/src/modules/tenant/actions.ts +46 -0
  261. package/src/modules/tenant/index.ts +3 -0
  262. package/src/{services → modules/tenant}/tenant.ts +3 -4
  263. package/src/modules/tenant/types.ts +12 -0
  264. package/tsconfig.json +4 -1
  265. package/dist/actions/nodes.d.ts +0 -4
  266. package/dist/actions/nodes.js +0 -15
  267. package/dist/enums/authentication-factor.d.ts +0 -6
  268. package/dist/enums/authentication-factor.js +0 -7
  269. package/dist/enums/authenticator-type.d.ts +0 -3
  270. package/dist/enums/authenticator-type.js +0 -4
  271. package/dist/enums/o-auth-provider.d.ts +0 -41
  272. package/dist/enums/o-auth-provider.js +0 -42
  273. package/dist/services/account.d.ts +0 -530
  274. package/dist/services/account.js +0 -1259
  275. package/dist/services/node.d.ts +0 -5
  276. package/dist/services/node.js +0 -11
  277. package/dist/services/tenant.js +0 -124
@@ -10,7 +10,7 @@ export const CreateUser = actionClient
10
10
  .schema(CreateUserSchema)
11
11
  .action(async ({ parsedInput }): Promise<User> => {
12
12
  try {
13
- const { userId, email, password, name } = parsedInput;
13
+ const { userId, email, password, name } = parsedInput as any;
14
14
  const { accounts } = await getSDKForCurrentUser();
15
15
  return await accounts.create(userId, email, password, name);
16
16
 
@@ -0,0 +1,55 @@
1
+
2
+ import { Payload } from "../../client";
3
+ import { ServiceClient } from "../../service-client";
4
+
5
+ type Blacklist = {
6
+ id: string
7
+ createdAt: Date
8
+ type: string
9
+ value: string
10
+ active: boolean
11
+ registerAttempts: number
12
+ }
13
+
14
+ export class Acl extends ServiceClient {
15
+
16
+ protected getServiceName(): string {
17
+ return 'com.appconda.service.acl';
18
+ }
19
+
20
+ public async getBlacklist(
21
+ filters?: any,
22
+ pagination?: { page: number; pageSize: number }
23
+ ): Promise<{ items: Blacklist[]; pagination: any }> {
24
+
25
+ const payload: Payload = {};
26
+
27
+ return await this.actionCall('GetBlackList', payload);
28
+
29
+ }
30
+
31
+ public async addToBlackList(type: string, value: string ): Promise<Blacklist> {
32
+
33
+ const payload: Payload = {};
34
+ payload['type'] = type;
35
+ payload['value'] = value;
36
+ return await this.actionCall('AddToBlackList', payload);
37
+
38
+ }
39
+
40
+ public async deleteFromBlackList(type: string, value: string ): Promise<boolean> {
41
+
42
+ const payload: Payload = {};
43
+ payload['type'] = type;
44
+ payload['value'] = value;
45
+ return await this.actionCall('DeleteFromBlackList', payload);
46
+
47
+ }
48
+
49
+ public async getAllTenantIpAddresses(): Promise<{ items: any[]; pagination: any }> {
50
+
51
+ const payload: Payload = {};
52
+ return await this.actionCall('GetAllTenantIpAddresses', payload);
53
+
54
+ }
55
+ }
@@ -75,3 +75,5 @@ export const ListModels = actionClient
75
75
  throw new Error('Failed to fetch ListModels');
76
76
  }
77
77
  });
78
+
79
+
@@ -1,6 +1,5 @@
1
1
  'use server';
2
2
 
3
- import { z } from "zod";
4
3
  import { actionClient } from "../../../actions/actionClient";
5
4
  import { getSDKForCurrentUser } from "../../../getSDKForCurrentUser";
6
5
 
@@ -0,0 +1,4 @@
1
+
2
+ export * from './lib/handler';
3
+
4
+ import './lib/jobs';
@@ -0,0 +1,103 @@
1
+
2
+ type TaskPayload = {
3
+ timestamp: Date;
4
+ lastTimestamp?: Date;
5
+ timezone: string;
6
+ };
7
+
8
+ type TaskContext = {
9
+ retriesLeft: number;
10
+ };
11
+
12
+ type TriggerArgs = {
13
+ id: string;
14
+ events: string[];
15
+ maxDuration?: number; // saniye
16
+ retries?: number;
17
+ run: (
18
+ payload: TaskPayload,
19
+ tools: { ctx: TaskContext }
20
+ ) => Promise<void> | void;
21
+ };
22
+
23
+ export class Hooks {
24
+ private lastRunTimestamps: Record<string, Date> = {};
25
+ private triggers: TriggerArgs[] = [];
26
+
27
+ public trigger(args: TriggerArgs) {
28
+ this.triggers.push(args);
29
+ }
30
+
31
+ public async run(hookId: string) {
32
+ const now = new Date();
33
+ const lastTimestamp = this.lastRunTimestamps[hookId];
34
+
35
+ const payload: TaskPayload = {
36
+ timestamp: now,
37
+ lastTimestamp,
38
+ timezone: "UTC",
39
+ };
40
+
41
+ this.lastRunTimestamps[hookId] = now;
42
+
43
+ const context: TaskContext = {
44
+ retriesLeft: this.triggers.find(hook => hook.id === hookId)?.retries ?? 0,
45
+ };
46
+
47
+ await this.runWithRetry(this.triggers.find(hook => hook.id === hookId)!, payload, context);
48
+
49
+
50
+ }
51
+
52
+ private async runWithRetry(
53
+ trigger: TriggerArgs,
54
+ payload: TaskPayload,
55
+ ctx: TaskContext
56
+ ) {
57
+ const start = Date.now();
58
+
59
+ try {
60
+ await Promise.race([
61
+ trigger.run(payload, { ctx }),
62
+ timeout(trigger.maxDuration ?? 60),
63
+ ]);
64
+ log("success", { taskId: trigger.id, time: new Date().toISOString() });
65
+ } catch (err) {
66
+ //@ts-ignore
67
+ log("error", { taskId: trigger.id, error: err });
68
+
69
+ if (ctx.retriesLeft > 0) {
70
+ ctx.retriesLeft -= 1;
71
+ //@ts-ignore
72
+ log("warn", { taskId: trigger.id, msg: "Retrying..." });
73
+ await this.runWithRetry(trigger, payload, ctx);
74
+ }
75
+ } finally {
76
+ const duration = Date.now() - start;
77
+ //@ts-ignore
78
+ log("info", { taskId: trigger.id, durationMs: duration });
79
+ }
80
+ }
81
+
82
+ public toJSON() {
83
+ return this.triggers.map(trigger => ({
84
+ id: trigger.id,
85
+ event: trigger.events,
86
+ maxDuration: trigger.maxDuration,
87
+ retries: trigger.retries,
88
+ }));
89
+ }
90
+ }
91
+
92
+ function log(arg0: string, arg1: { taskId: string; time: string; }) {
93
+ // console.log(arg0, arg1);
94
+ }
95
+
96
+ function timeout(seconds: number) {
97
+ return new Promise((_, reject) =>
98
+ setTimeout(() => reject(new Error("Timeout reached")), seconds * 1000)
99
+ );
100
+ }
101
+
102
+
103
+ export const hooks = new Hooks();
@@ -0,0 +1,27 @@
1
+ import { NextRequest, NextResponse } from 'next/server';
2
+
3
+ import './jobs';
4
+ import { hooks } from './Hooks';
5
+
6
+ /* export async function GET() {
7
+ return NextResponse.json({ schedules: schedules.toJSON() },
8
+ { status: 200 });
9
+ }
10
+
11
+ export async function POST(request: Request) {
12
+
13
+ } */
14
+
15
+ export async function hookHandler(request: NextRequest, res: NextResponse) {
16
+ if (request.method === 'POST') {
17
+ const body = await request.json();
18
+ const hookId = body.hookId;
19
+ hooks.run(hookId);
20
+ return NextResponse.json({ message: 'Callback triggered' },
21
+ { status: 200 });
22
+ } else {
23
+ return NextResponse.json({ hooks: hooks.toJSON() },
24
+ { status: 200 });
25
+ }
26
+ }
27
+
@@ -0,0 +1,2 @@
1
+ import './log-job';
2
+ import './job1';
@@ -0,0 +1,35 @@
1
+ import { schedules } from "../../../scheduled-job";
2
+ import { hooks } from "../Hooks";
3
+
4
+ hooks.trigger({
5
+ id: "job1",
6
+ events: ["event1"],
7
+ run: async () => {
8
+ console.log("hello from job 1");
9
+ },
10
+ });
11
+
12
+
13
+ hooks.trigger({
14
+ id: "job2",
15
+ events: ["event2"],
16
+ run: async () => {
17
+ console.log("hello from job 2");
18
+ },
19
+ });
20
+
21
+ schedules.task({
22
+ id: "job3",
23
+ cron: "*/5 * * * *",
24
+ run: async () => {
25
+ console.log("hello from job 3");
26
+ },
27
+ });
28
+
29
+ schedules.task({
30
+ id: "job4",
31
+ cron: "*/5 * * * *",
32
+ run: async () => {
33
+ console.log("hello from job 4");
34
+ },
35
+ });
@@ -0,0 +1,9 @@
1
+ import { schedules } from "../Hooks";
2
+
3
+ schedules.task({
4
+ id: "test",
5
+ cron: "*/50 * * * * *",
6
+ run: async () => {
7
+ console.log("test");
8
+ },
9
+ });
@@ -0,0 +1,17 @@
1
+ export const wait = {
2
+ for: ({ seconds }: { seconds: number }) =>
3
+ new Promise((resolve) => setTimeout(resolve, seconds * 1000)),
4
+
5
+ until: ({ date }: { date: Date }) => {
6
+ const now = new Date();
7
+ const diff = date.getTime() - now.getTime();
8
+
9
+ if (diff <= 0) {
10
+ // Hedef tarih zaten geçmişse hiç bekleme
11
+ return Promise.resolve();
12
+ }
13
+
14
+ return new Promise((resolve) => setTimeout(resolve, diff));
15
+ },
16
+ };
17
+
@@ -1,4 +1,8 @@
1
1
  export * from './waitlist';
2
2
  export * from './account';
3
3
  export * from './ai';
4
- export * from './agent';
4
+ export * from './agent';
5
+ export * from './tenant';
6
+ export * from './task';
7
+ export * from './scheduled-job';
8
+ export * from './hooks';
@@ -0,0 +1,94 @@
1
+ 'use server';
2
+
3
+
4
+ import { actionClient } from '../../actions/actionClient';
5
+ import { getSDKForCurrentUser } from '../../getSDKForCurrentUser';
6
+ import { CreateScheduledJobSchema, GetScheduledJobSchema, ListAvgExecutionsSchema, ListExecutionsSchema, ListScheduledJobsSchema, UpdateJobSchema } from './schema';
7
+ import { AvgExecution, Execution, ScheduledJob } from './types';
8
+
9
+ export const CreateScheduledJob = actionClient
10
+ .schema(CreateScheduledJobSchema)
11
+ .action(async ({ parsedInput }): Promise<ScheduledJob> => {
12
+ try {
13
+
14
+ const { scheduledJob } = await getSDKForCurrentUser();
15
+ return await scheduledJob.CreateScheduledJob(parsedInput);
16
+
17
+ } catch (error) {
18
+ console.error('Error in CreateScheduledJob:', error);
19
+ throw new Error('Failed to fetch CreateScheduledJob');
20
+ }
21
+ });
22
+
23
+ export const ListScheduledJobs = actionClient
24
+ .schema(ListScheduledJobsSchema)
25
+ .action(async ({ parsedInput }): Promise<ScheduledJob[]> => {
26
+ try {
27
+
28
+ const { scheduledJob } = await getSDKForCurrentUser();
29
+ return await scheduledJob.ListScheduledJobs(parsedInput);
30
+
31
+ } catch (error) {
32
+ console.error('Error in ListScheduledJobs:', error);
33
+ throw new Error('Failed to fetch ListScheduledJobs');
34
+ }
35
+ });
36
+
37
+ export const GetScheduledJob = actionClient
38
+ .schema(GetScheduledJobSchema)
39
+ .action(async ({ parsedInput }): Promise<ScheduledJob> => {
40
+ try {
41
+
42
+ const { scheduledJob } = await getSDKForCurrentUser();
43
+ return await scheduledJob.GetScheduledJob(parsedInput);
44
+
45
+ } catch (error) {
46
+ console.error('Error in GetScheduledJob:', error);
47
+ throw new Error('Failed to fetch GetScheduledJob');
48
+ }
49
+ });
50
+
51
+ export const ListExecutions = actionClient
52
+ .schema(ListExecutionsSchema)
53
+ .action(async ({ parsedInput }): Promise<Execution[]> => {
54
+ try {
55
+
56
+ const { scheduledJob } = await getSDKForCurrentUser();
57
+ return await scheduledJob.ListExecutions(parsedInput);
58
+
59
+ } catch (error) {
60
+ console.error('Error in ListExecutions:', error);
61
+ throw new Error('Failed to fetch ListExecutions');
62
+ }
63
+ });
64
+
65
+ export const ListAvgExecutions = actionClient
66
+ .schema(ListAvgExecutionsSchema)
67
+ .action(async ({ parsedInput }): Promise<AvgExecution[]> => {
68
+ try {
69
+
70
+ const { scheduledJob } = await getSDKForCurrentUser();
71
+ return await scheduledJob.ListAvgExecutions(parsedInput);
72
+
73
+ } catch (error) {
74
+ console.error('Error in ListAvgExecutions:', error);
75
+ throw new Error('Failed to fetch ListAvgExecutions');
76
+ }
77
+ });
78
+
79
+ export const UpdateJob = actionClient
80
+ .schema(UpdateJobSchema)
81
+ .action(async ({ parsedInput }): Promise<ScheduledJob> => {
82
+ try {
83
+
84
+ const { scheduledJob } = await getSDKForCurrentUser();
85
+ return await scheduledJob.UpdateJob(parsedInput);
86
+
87
+ } catch (error) {
88
+ console.error('Error in UpdateJob:', error);
89
+ throw new Error('Failed to fetch UpdateJob');
90
+ }
91
+ });
92
+
93
+
94
+
@@ -0,0 +1,8 @@
1
+ export * from './action';
2
+ export * from './schema';
3
+ export * from './service';
4
+ export * from './types';
5
+ export * from './lib/Schedules';
6
+ export * from './lib/handler';
7
+
8
+ import './lib/jobs';
@@ -0,0 +1,103 @@
1
+
2
+ type TaskPayload = {
3
+ timestamp: Date;
4
+ lastTimestamp?: Date;
5
+ timezone: string;
6
+ };
7
+
8
+ type TaskContext = {
9
+ retriesLeft: number;
10
+ };
11
+
12
+ type TaskArgs = {
13
+ id: string;
14
+ cron: string;
15
+ maxDuration?: number; // saniye
16
+ retries?: number;
17
+ run: (
18
+ payload: TaskPayload,
19
+ tools: { ctx: TaskContext }
20
+ ) => Promise<void> | void;
21
+ };
22
+
23
+ export class Schedules {
24
+ private lastRunTimestamps: Record<string, Date> = {};
25
+ private Tasks: TaskArgs[] = [];
26
+
27
+ public task(args: TaskArgs) {
28
+ this.Tasks.push(args);
29
+ }
30
+
31
+ public async run(taskId: string) {
32
+ const now = new Date();
33
+ const lastTimestamp = this.lastRunTimestamps[taskId];
34
+
35
+ const payload: TaskPayload = {
36
+ timestamp: now,
37
+ lastTimestamp,
38
+ timezone: "UTC",
39
+ };
40
+
41
+ this.lastRunTimestamps[taskId] = now;
42
+
43
+ const context: TaskContext = {
44
+ retriesLeft: this.Tasks.find(task => task.id === taskId)?.retries ?? 0,
45
+ };
46
+
47
+ await this.runWithRetry(this.Tasks.find(task => task.id === taskId)!, payload, context);
48
+
49
+
50
+ }
51
+
52
+ private async runWithRetry(
53
+ task: TaskArgs,
54
+ payload: TaskPayload,
55
+ ctx: TaskContext
56
+ ) {
57
+ const start = Date.now();
58
+
59
+ try {
60
+ await Promise.race([
61
+ task.run(payload, { ctx }),
62
+ timeout(task.maxDuration ?? 60),
63
+ ]);
64
+ log("success", { taskId: task.id, time: new Date().toISOString() });
65
+ } catch (err) {
66
+ //@ts-ignore
67
+ log("error", { taskId: task.id, error: err });
68
+
69
+ if (ctx.retriesLeft > 0) {
70
+ ctx.retriesLeft -= 1;
71
+ //@ts-ignore
72
+ log("warn", { taskId: task.id, msg: "Retrying..." });
73
+ await this.runWithRetry(task, payload, ctx);
74
+ }
75
+ } finally {
76
+ const duration = Date.now() - start;
77
+ //@ts-ignore
78
+ log("info", { taskId: task.id, durationMs: duration });
79
+ }
80
+ }
81
+
82
+ public toJSON() {
83
+ return this.Tasks.map(task => ({
84
+ id: task.id,
85
+ cron: task.cron,
86
+ maxDuration: task.maxDuration,
87
+ retries: task.retries,
88
+ }));
89
+ }
90
+ }
91
+
92
+ function log(arg0: string, arg1: { taskId: string; time: string; }) {
93
+ // console.log(arg0, arg1);
94
+ }
95
+
96
+ function timeout(seconds: number) {
97
+ return new Promise((_, reject) =>
98
+ setTimeout(() => reject(new Error("Timeout reached")), seconds * 1000)
99
+ );
100
+ }
101
+
102
+
103
+ export const schedules = new Schedules();
@@ -0,0 +1,27 @@
1
+ import { NextRequest, NextResponse } from 'next/server';
2
+
3
+ import './jobs';
4
+ import { schedules } from './Schedules';
5
+
6
+ /* export async function GET() {
7
+ return NextResponse.json({ schedules: schedules.toJSON() },
8
+ { status: 200 });
9
+ }
10
+
11
+ export async function POST(request: Request) {
12
+
13
+ } */
14
+
15
+ export async function scheduledJobHandler(request: NextRequest, res: NextResponse) {
16
+ if (request.method === 'POST') {
17
+ const body = await request.json();
18
+ const taskId = body.taskId;
19
+ schedules.run(taskId);
20
+ return NextResponse.json({ message: 'Callback triggered' },
21
+ { status: 200 });
22
+ } else {
23
+ return NextResponse.json({ schedules: schedules.toJSON() },
24
+ { status: 200 });
25
+ }
26
+ }
27
+
@@ -0,0 +1,2 @@
1
+ import './log-job';
2
+ import './job1';
@@ -0,0 +1,34 @@
1
+ import { schedules } from "../Schedules";
2
+
3
+ schedules.task({
4
+ id: "job1",
5
+ cron: "*/5 * * * *",
6
+ run: async () => {
7
+ console.log("hello from job 1");
8
+ },
9
+ });
10
+
11
+
12
+ schedules.task({
13
+ id: "job2",
14
+ cron: "*/5 * * * *",
15
+ run: async () => {
16
+ console.log("hello from job 2");
17
+ },
18
+ });
19
+
20
+ schedules.task({
21
+ id: "job3",
22
+ cron: "*/5 * * * *",
23
+ run: async () => {
24
+ console.log("hello from job 3");
25
+ },
26
+ });
27
+
28
+ schedules.task({
29
+ id: "job4",
30
+ cron: "*/5 * * * *",
31
+ run: async () => {
32
+ console.log("hello from job 4");
33
+ },
34
+ });
@@ -0,0 +1,9 @@
1
+ import { schedules } from "../Schedules";
2
+
3
+ schedules.task({
4
+ id: "test",
5
+ cron: "*/50 * * * * *",
6
+ run: async () => {
7
+ console.log("test");
8
+ },
9
+ });
@@ -0,0 +1,17 @@
1
+ export const wait = {
2
+ for: ({ seconds }: { seconds: number }) =>
3
+ new Promise((resolve) => setTimeout(resolve, seconds * 1000)),
4
+
5
+ until: ({ date }: { date: Date }) => {
6
+ const now = new Date();
7
+ const diff = date.getTime() - now.getTime();
8
+
9
+ if (diff <= 0) {
10
+ // Hedef tarih zaten geçmişse hiç bekleme
11
+ return Promise.resolve();
12
+ }
13
+
14
+ return new Promise((resolve) => setTimeout(resolve, diff));
15
+ },
16
+ };
17
+
@@ -0,0 +1,31 @@
1
+ import { z } from "zod";
2
+
3
+ export const CreateScheduledJobSchema = z.object({
4
+ id: z.string().optional(),
5
+ name: z.string(),
6
+ cron: z.string(),
7
+ enabled: z.boolean().optional().default(true),
8
+ });
9
+
10
+ export const ListScheduledJobsSchema = z.object({
11
+
12
+ });
13
+
14
+ export const GetScheduledJobSchema = z.object({
15
+ id: z.string()
16
+ });
17
+
18
+ export const ListExecutionsSchema = z.object({
19
+ scheduledJobId: z.string(),
20
+ status: z.string().optional()
21
+ });
22
+
23
+ export const ListAvgExecutionsSchema = z.object({
24
+ });
25
+
26
+ export const UpdateJobSchema = z.object({
27
+ id: z.string(),
28
+ name: z.string().optional(),
29
+ cron: z.string().optional(),
30
+ enabled: z.boolean().optional(),
31
+ });
@@ -0,0 +1,37 @@
1
+
2
+ import z from "zod";
3
+ import { ServiceClient } from "../../service-client";
4
+ import { CreateScheduledJobSchema, ListScheduledJobsSchema, GetScheduledJobSchema, ListExecutionsSchema, ListAvgExecutionsSchema, UpdateJobSchema } from "./schema";
5
+ import { AvgExecution, Execution, ScheduledJob } from "./types";
6
+
7
+ export class ScheduledJobService extends ServiceClient {
8
+ protected getServiceName(): string {
9
+ return 'com.appconda.service.scheduled-jobs';
10
+ }
11
+
12
+ public async CreateScheduledJob(payload: z.infer<typeof CreateScheduledJobSchema>): Promise<ScheduledJob> {
13
+ return await this.actionCall('CreateScheduledJob', payload);
14
+ }
15
+
16
+ public async ListScheduledJobs(payload: z.infer<typeof ListScheduledJobsSchema>): Promise<ScheduledJob[]> {
17
+ return await this.actionCall('ListScheduledJobs', payload);
18
+ }
19
+
20
+ public async GetScheduledJob(payload: z.infer<typeof GetScheduledJobSchema>): Promise<ScheduledJob> {
21
+ return await this.actionCall('GetScheduledJob', payload);
22
+ }
23
+
24
+ public async ListExecutions(payload: z.infer<typeof ListExecutionsSchema>): Promise<Execution[]> {
25
+ return await this.actionCall('ListExecutions', payload);
26
+ }
27
+
28
+ public async ListAvgExecutions(payload: z.infer<typeof ListAvgExecutionsSchema>): Promise<AvgExecution[]> {
29
+ return await this.actionCall('ListAvgExecutions', payload);
30
+ }
31
+
32
+ public async UpdateJob(payload: z.infer<typeof UpdateJobSchema>): Promise<ScheduledJob> {
33
+ return await this.actionCall('UpdateJob', payload);
34
+ }
35
+
36
+
37
+ }