@arken/node 1.5.1 → 1.5.2

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 (148) hide show
  1. package/db.ts +76 -1
  2. package/index.ts +351 -18
  3. package/package.json +3 -3
  4. package/tsconfig.json +33 -2
  5. package/util.ts +1 -0
  6. package/modules/area/area.models.ts +0 -15
  7. package/modules/area/area.router.ts +0 -74
  8. package/modules/area/area.schema.ts +0 -22
  9. package/modules/area/area.service.ts +0 -124
  10. package/modules/area/area.types.ts +0 -26
  11. package/modules/area/index.ts +0 -5
  12. package/modules/asset/asset.models.ts +0 -59
  13. package/modules/asset/asset.router.ts +0 -55
  14. package/modules/asset/asset.schema.ts +0 -27
  15. package/modules/asset/asset.service.ts +0 -85
  16. package/modules/asset/asset.types.ts +0 -22
  17. package/modules/asset/index.ts +0 -5
  18. package/modules/chain/chain.models.ts +0 -50
  19. package/modules/chain/chain.router.ts +0 -104
  20. package/modules/chain/chain.schema.ts +0 -52
  21. package/modules/chain/chain.service.ts +0 -167
  22. package/modules/chain/chain.types.ts +0 -24
  23. package/modules/chain/index.ts +0 -5
  24. package/modules/character/character.models.ts +0 -174
  25. package/modules/character/character.router.ts +0 -314
  26. package/modules/character/character.schema.ts +0 -147
  27. package/modules/character/character.service.ts +0 -876
  28. package/modules/character/character.types.ts +0 -64
  29. package/modules/character/index.ts +0 -5
  30. package/modules/chat/chat.models.ts +0 -43
  31. package/modules/chat/chat.router.ts +0 -67
  32. package/modules/chat/chat.schema.ts +0 -36
  33. package/modules/chat/chat.service.ts +0 -128
  34. package/modules/chat/chat.types.ts +0 -20
  35. package/modules/chat/index.ts +0 -5
  36. package/modules/collection/collection.models.ts +0 -76
  37. package/modules/collection/collection.router.ts +0 -91
  38. package/modules/collection/collection.schema.ts +0 -90
  39. package/modules/collection/collection.service.ts +0 -192
  40. package/modules/collection/collection.types.ts +0 -36
  41. package/modules/collection/index.ts +0 -5
  42. package/modules/core/core.models.ts +0 -1380
  43. package/modules/core/core.router.ts +0 -1781
  44. package/modules/core/core.schema.ts +0 -847
  45. package/modules/core/core.service.ts +0 -2824
  46. package/modules/core/core.types.ts +0 -340
  47. package/modules/core/index.ts +0 -5
  48. package/modules/core/mail/applyPatchesOrMail.ts +0 -568
  49. package/modules/core/mail/mailClaimablePatchesBatch.ts +0 -381
  50. package/modules/game/game.models.ts +0 -53
  51. package/modules/game/game.router.ts +0 -110
  52. package/modules/game/game.schema.ts +0 -23
  53. package/modules/game/game.service.ts +0 -143
  54. package/modules/game/game.types.ts +0 -28
  55. package/modules/game/index.ts +0 -5
  56. package/modules/interface/index.ts +0 -5
  57. package/modules/interface/interface.canonicalize.ts +0 -279
  58. package/modules/interface/interface.models.ts +0 -40
  59. package/modules/interface/interface.router.ts +0 -175
  60. package/modules/interface/interface.schema.ts +0 -59
  61. package/modules/interface/interface.service.ts +0 -356
  62. package/modules/interface/interface.types.ts +0 -25
  63. package/modules/item/index.ts +0 -5
  64. package/modules/item/item.models.ts +0 -124
  65. package/modules/item/item.router.ts +0 -103
  66. package/modules/item/item.schema.ts +0 -120
  67. package/modules/item/item.service.ts +0 -167
  68. package/modules/item/item.types.ts +0 -74
  69. package/modules/job/index.ts +0 -5
  70. package/modules/job/job.models.ts +0 -14
  71. package/modules/job/job.router.ts +0 -44
  72. package/modules/job/job.schema.ts +0 -9
  73. package/modules/job/job.service.ts +0 -243
  74. package/modules/job/job.types.ts +0 -23
  75. package/modules/market/index.ts +0 -5
  76. package/modules/market/market.models.ts +0 -113
  77. package/modules/market/market.router.ts +0 -73
  78. package/modules/market/market.schema.ts +0 -140
  79. package/modules/market/market.service.ts +0 -122
  80. package/modules/market/market.types.ts +0 -56
  81. package/modules/product/index.ts +0 -5
  82. package/modules/product/product.models.ts +0 -166
  83. package/modules/product/product.router.ts +0 -93
  84. package/modules/product/product.schema.ts +0 -149
  85. package/modules/product/product.service.ts +0 -160
  86. package/modules/product/product.types.ts +0 -33
  87. package/modules/profile/index.ts +0 -5
  88. package/modules/profile/profile.models.ts +0 -214
  89. package/modules/profile/profile.router.ts +0 -72
  90. package/modules/profile/profile.schema.ts +0 -156
  91. package/modules/profile/profile.service.ts +0 -149
  92. package/modules/profile/profile.types.ts +0 -22
  93. package/modules/raffle/index.ts +0 -5
  94. package/modules/raffle/raffle.models.ts +0 -44
  95. package/modules/raffle/raffle.router.ts +0 -90
  96. package/modules/raffle/raffle.schema.ts +0 -32
  97. package/modules/raffle/raffle.service.ts +0 -167
  98. package/modules/raffle/raffle.types.ts +0 -30
  99. package/modules/skill/index.ts +0 -5
  100. package/modules/skill/skill.models.ts +0 -16
  101. package/modules/skill/skill.router.ts +0 -201
  102. package/modules/skill/skill.schema.ts +0 -40
  103. package/modules/skill/skill.service.ts +0 -390
  104. package/modules/skill/skill.types.ts +0 -33
  105. package/modules/video/index.ts +0 -5
  106. package/modules/video/video.models.ts +0 -25
  107. package/modules/video/video.router.ts +0 -143
  108. package/modules/video/video.schema.ts +0 -46
  109. package/modules/video/video.service.ts +0 -274
  110. package/modules/video/video.types.ts +0 -33
  111. package/util/db/index.ts +0 -7
  112. package/util/db/isPostgresError.ts +0 -9
  113. package/util/db/isUniqueConstraintViolation.ts +0 -3
  114. package/util/db.ts +0 -62
  115. package/util/index.ts +0 -351
  116. /package/{util/api.ts → api.ts} +0 -0
  117. /package/{util/array.ts → array.ts} +0 -0
  118. /package/{util/browser.ts → browser.ts} +0 -0
  119. /package/{util/codebase.ts → codebase.ts} +0 -0
  120. /package/{util/config.ts → config.ts} +0 -0
  121. /package/{util/decoder.test.ts → decoder.test.ts} +0 -0
  122. /package/{util/decoder.ts → decoder.ts} +0 -0
  123. /package/{util/format.ts → format.ts} +0 -0
  124. /package/{util/guid.ts → guid.ts} +0 -0
  125. /package/{util/json.ts → json.ts} +0 -0
  126. /package/{util/log.ts → log.ts} +0 -0
  127. /package/{util/math.ts → math.ts} +0 -0
  128. /package/{util/merkle.ts → merkle.ts} +0 -0
  129. /package/{util/mongo.ts → mongo.ts} +0 -0
  130. /package/{util/number.ts → number.ts} +0 -0
  131. /package/{util/object.ts → object.ts} +0 -0
  132. /package/{util/otp.ts → otp.ts} +0 -0
  133. /package/{util/physics.ts → physics.ts} +0 -0
  134. /package/{util/process.ts → process.ts} +0 -0
  135. /package/{util/rpc.ts → rpc.ts} +0 -0
  136. /package/{util/seer.ts → seer.ts} +0 -0
  137. /package/{util/string.ts → string.ts} +0 -0
  138. /package/{util/text.ts → text.ts} +0 -0
  139. /package/{util/time → time}/date.ts +0 -0
  140. /package/{util/time → time}/fancyTimeFormat.ts +0 -0
  141. /package/{util/time → time}/index.ts +0 -0
  142. /package/{util/time → time}/now.ts +0 -0
  143. /package/{util/types → types}/mongo.d.ts +0 -0
  144. /package/{util/web3 → web3}/httpProvider.ts +0 -0
  145. /package/{util/web3.ts → web3.ts} +0 -0
  146. /package/{util/websocket.ts → websocket.ts} +0 -0
  147. /package/{util/zk.ts → zk.ts} +0 -0
  148. /package/{util/zod.ts → zod.ts} +0 -0
@@ -1,120 +0,0 @@
1
- import { z, ObjectId, Entity } from '../../schema';
2
-
3
- export const ItemAttribute = Entity.merge(
4
- z.object({
5
- // Define fields for ItemAttribute here if needed
6
- })
7
- );
8
-
9
- export const ItemMaterial = Entity.merge(
10
- z.object({
11
- // Define fields for ItemMaterial here if needed
12
- })
13
- );
14
-
15
- export const ItemSet = Entity.merge(
16
- z.object({
17
- // Define fields for ItemSet here if needed
18
- })
19
- );
20
-
21
- export const ItemSlot = Entity.merge(
22
- z.object({
23
- // Define fields for ItemSlot here if needed
24
- })
25
- );
26
-
27
- export const ItemRarity = Entity.merge(
28
- z.object({
29
- // Define fields for ItemRarity here if needed
30
- })
31
- );
32
-
33
- export const ItemRank = Entity.merge(
34
- z.object({
35
- value: z.number(),
36
- // Define fields for ItemRank here if needed
37
- })
38
- );
39
-
40
- export const ItemType = Entity.merge(
41
- z.object({
42
- // Define fields for ItemType here if needed
43
- })
44
- );
45
-
46
- export const ItemSubType = Entity.merge(
47
- z.object({
48
- // Define fields for ItemSubType here if needed
49
- })
50
- );
51
-
52
- export const ItemSpecificType = Entity.merge(
53
- z.object({
54
- // Define fields for ItemSpecificType here if needed
55
- })
56
- );
57
-
58
- export const ItemAffix = Entity.merge(
59
- z.object({
60
- isPrefix: z.boolean().default(false),
61
- isSuffix: z.boolean().default(false),
62
- isTitle: z.boolean().default(false),
63
- weight: z.number().min(0).default(1),
64
- typeIds: z.array(ObjectId).optional(),
65
- rarityIds: z.array(ObjectId).optional(),
66
- })
67
- );
68
-
69
- export const ItemRecipe = Entity.merge(
70
- z.object({
71
- // Define fields for ItemRecipe here if needed
72
- })
73
- );
74
-
75
- export const ItemSkin = Entity.merge(
76
- z.object({
77
- // Define fields for ItemSkin here if needed
78
- })
79
- );
80
-
81
- export const ItemTransmute = Entity.merge(
82
- z.object({
83
- token: z.string().max(500).min(1),
84
- assetId: ObjectId,
85
- itemId: ObjectId,
86
- chainId: ObjectId.optional(),
87
- })
88
- );
89
-
90
- export const Item = Entity.merge(
91
- z.object({
92
- characterId: ObjectId.optional(),
93
- assetId: ObjectId.optional(),
94
- chainId: ObjectId.optional(),
95
- materialId: ItemMaterial.optional(),
96
- skinId: ItemSkin.optional(),
97
- recipeId: ItemRecipe.optional(),
98
- typeId: ItemType.optional(),
99
- subTypeId: ItemSubType.optional(),
100
- specificTypeId: ItemSpecificType.optional(),
101
- rankId: ItemRank.optional(),
102
- rankValue: z.number(),
103
- rarityId: ItemRarity.optional(),
104
- slotIds: z.array(ItemSlot.optional()),
105
- setId: ItemSet.optional(),
106
- attributes: z.array(ItemAttribute).optional(),
107
- token: z.string().max(500).min(1).optional(),
108
- quantity: z.number().int().nonnegative().default(1),
109
- x: z.number().int().nonnegative().optional(),
110
- y: z.number().int().nonnegative().optional(),
111
- distribution: z
112
- .enum(['Unknown', 'Found', 'Fundraiser', 'Claimed', 'Crafted', 'Airdrop', 'Reward', 'Farmed', 'Migration'])
113
- .default('Unknown'),
114
- // properties: z.record(z.any()).optional(),
115
- // type: z.string().default('bag'), // stash, bag, equipment, etc.
116
- items: z.array(z.lazy(() => Item)).default([]),
117
- capacity: z.number().int().nonnegative().default(60),
118
- points: z.number().int().nonnegative().default(0),
119
- })
120
- );
@@ -1,167 +0,0 @@
1
- import type {
2
- Item,
3
- ItemAttribute,
4
- ItemMaterial,
5
- ItemSet,
6
- ItemSlot,
7
- ItemRarity,
8
- ItemType,
9
- ItemSubType,
10
- ItemSpecificType,
11
- ItemAffix,
12
- ItemRecipe,
13
- ItemSkin,
14
- ItemTransmute,
15
- RouterContext,
16
- Router,
17
- RouterInput,
18
- RouterOutput,
19
- } from './item.types';
20
- import { getFilter } from '../../util/api';
21
- import { ARXError } from '../../util/rpc';
22
-
23
- export class Service {
24
- async getItem(input: RouterInput['getItem'], ctx: RouterContext): Promise<RouterOutput['getItem']> {
25
- if (!input) throw new ARXError('NO_INPUT');
26
- console.log('Item.Service.getItem', input);
27
-
28
- const filter = getFilter(input);
29
- const item = await ctx.app.model.Item.findById(filter.id).lean().exec();
30
- if (!item) throw new Error('Item not found');
31
-
32
- return item as Item;
33
- }
34
-
35
- async getItems(input: RouterInput['getItems'], ctx: RouterContext): Promise<RouterOutput['getItems']> {
36
- if (!input) throw new ARXError('NO_INPUT');
37
- const filter = getFilter(input);
38
-
39
- // Optional ordering so $first picks the right representative per token
40
- const sortStage = {
41
- $sort: {
42
- updatedDate: -1, // or createdDate, or anything that establishes “latest”
43
- _id: -1, // tiebreaker for stable results
44
- },
45
- };
46
-
47
- const pipeline: any[] = [
48
- { $match: filter },
49
- sortStage,
50
- { $group: { _id: '$token', doc: { $first: '$$ROOT' } } },
51
- { $replaceRoot: { newRoot: '$doc' } },
52
- { $skip: input.skip || 0 },
53
- { $limit: input.limit || 10 },
54
- ];
55
-
56
- const items = await ctx.app.model.Item.aggregate(pipeline).exec();
57
- return items as Item[];
58
- }
59
-
60
- async createItem(input: RouterInput['createItem'], ctx: RouterContext): Promise<RouterOutput['createItem']> {
61
- if (!input) throw new ARXError('NO_INPUT');
62
- console.log('Item.Service.createItem', input);
63
-
64
- const item = await ctx.app.model.Item.create(input);
65
- return item as Item;
66
- }
67
-
68
- async updateItem(input: RouterInput['updateItem'], ctx: RouterContext): Promise<RouterOutput['updateItem']> {
69
- if (!input) throw new ARXError('NO_INPUT');
70
- console.log('Item.Service.updateItem', input);
71
-
72
- const filter = getFilter(input);
73
- const updatedItem = await ctx.app.model.Item.findByIdAndUpdate(filter.id, input.data, { new: true }).lean().exec();
74
- if (!updatedItem) throw new Error('Item update failed');
75
-
76
- return updatedItem as Item;
77
- }
78
-
79
- async getItemAttribute(
80
- input: RouterInput['getItemAttribute'],
81
- ctx: RouterContext
82
- ): Promise<RouterOutput['getItemAttribute']> {
83
- if (!input) throw new ARXError('NO_INPUT');
84
- console.log('Item.Service.getItemAttribute', input);
85
-
86
- const filter = getFilter(input);
87
- const itemAttribute = await ctx.app.model.ItemAttribute.findById(filter.id).lean().exec();
88
- if (!itemAttribute) throw new Error('ItemAttribute not found');
89
-
90
- return itemAttribute as ItemAttribute;
91
- }
92
-
93
- async createItemAttribute(
94
- input: RouterInput['createItemAttribute'],
95
- ctx: RouterContext
96
- ): Promise<RouterOutput['createItemAttribute']> {
97
- if (!input) throw new ARXError('NO_INPUT');
98
- console.log('Item.Service.createItemAttribute', input);
99
-
100
- const filter = getFilter(input);
101
- const itemAttribute = await ctx.app.model.ItemAttribute.create(filter);
102
- return itemAttribute as ItemAttribute;
103
- }
104
-
105
- async updateItemAttribute(
106
- input: RouterInput['updateItemAttribute'],
107
- ctx: RouterContext
108
- ): Promise<RouterOutput['updateItemAttribute']> {
109
- if (!input) throw new ARXError('NO_INPUT');
110
- console.log('Item.Service.updateItemAttribute', input);
111
-
112
- const filter = getFilter(input);
113
- const updatedItemAttribute = await ctx.app.model.ItemAttribute.findByIdAndUpdate(filter.id, input.data, {
114
- new: true,
115
- })
116
- .lean()
117
- .exec();
118
- if (!updatedItemAttribute) throw new Error('ItemAttribute update failed');
119
-
120
- return updatedItemAttribute as ItemAttribute;
121
- }
122
-
123
- // Add similar methods for other entities like ItemMaterial, ItemSet, ItemSlot, ItemRarity, etc.
124
-
125
- async getItemTransmute(
126
- input: RouterInput['getItemTransmute'],
127
- ctx: RouterContext
128
- ): Promise<RouterOutput['getItemTransmute']> {
129
- if (!input) throw new ARXError('NO_INPUT');
130
- console.log('Item.Service.getItemTransmute', input);
131
-
132
- const filter = getFilter(input);
133
- const itemTransmute = await ctx.app.model.ItemTransmute.findById(filter.id).lean().exec();
134
- if (!itemTransmute) throw new Error('ItemTransmute not found');
135
-
136
- return itemTransmute as ItemTransmute;
137
- }
138
-
139
- async createItemTransmute(
140
- input: RouterInput['createItemTransmute'],
141
- ctx: RouterContext
142
- ): Promise<RouterOutput['createItemTransmute']> {
143
- if (!input) throw new ARXError('NO_INPUT');
144
- console.log('Item.Service.createItemTransmute', input);
145
-
146
- const itemTransmute = await ctx.app.model.ItemTransmute.create(input.data);
147
- return itemTransmute as ItemTransmute;
148
- }
149
-
150
- async updateItemTransmute(
151
- input: RouterInput['updateItemTransmute'],
152
- ctx: RouterContext
153
- ): Promise<RouterOutput['updateItemTransmute']> {
154
- if (!input) throw new ARXError('NO_INPUT');
155
- console.log('Item.Service.updateItemTransmute', input);
156
-
157
- const filter = getFilter(input);
158
- const updatedItemTransmute = await ctx.app.model.ItemTransmute.findByIdAndUpdate(filter.id, input.data, {
159
- new: true,
160
- })
161
- .lean()
162
- .exec();
163
- if (!updatedItemTransmute) throw new Error('ItemTransmute update failed');
164
-
165
- return updatedItemTransmute as ItemTransmute;
166
- }
167
- }
@@ -1,74 +0,0 @@
1
- // module/item.types.ts
2
-
3
- import { z } from 'zod';
4
- import * as schema from './item.schema';
5
- import { Document, Model } from '../../util/mongo';
6
- import type { RouterContext } from '../../types';
7
- import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server';
8
- import type { Router } from './item.router';
9
-
10
- export type * from './item.router';
11
- export type * from './item.service';
12
- export type { RouterContext };
13
-
14
- export type Item = z.infer<typeof schema.Item>;
15
- export type ItemDocument = Item & Document;
16
-
17
- export type ItemAttribute = z.infer<typeof schema.ItemAttribute>;
18
- export type ItemAttributeDocument = ItemAttribute & Document;
19
-
20
- export type ItemMaterial = z.infer<typeof schema.ItemMaterial>;
21
- export type ItemMaterialDocument = ItemMaterial & Document;
22
-
23
- export type ItemSet = z.infer<typeof schema.ItemSet>;
24
- export type ItemSetDocument = ItemSet & Document;
25
-
26
- export type ItemSlot = z.infer<typeof schema.ItemSlot>;
27
- export type ItemSlotDocument = ItemSlot & Document;
28
-
29
- export type ItemRarity = z.infer<typeof schema.ItemRarity>;
30
- export type ItemRarityDocument = ItemRarity & Document;
31
-
32
- export type ItemRank = z.infer<typeof schema.ItemRank>;
33
- export type ItemRankDocument = ItemRank & Document;
34
-
35
- export type ItemType = z.infer<typeof schema.ItemType>;
36
- export type ItemTypeDocument = ItemType & Document;
37
-
38
- export type ItemSubType = z.infer<typeof schema.ItemSubType>;
39
- export type ItemSubTypeDocument = ItemSubType & Document;
40
-
41
- export type ItemSpecificType = z.infer<typeof schema.ItemSpecificType>;
42
- export type ItemSpecificTypeDocument = ItemSpecificType & Document;
43
-
44
- export type ItemAffix = z.infer<typeof schema.ItemAffix>;
45
- export type ItemAffixDocument = ItemAffix & Document;
46
-
47
- export type ItemRecipe = z.infer<typeof schema.ItemRecipe>;
48
- export type ItemRecipeDocument = ItemRecipe & Document;
49
-
50
- export type ItemSkin = z.infer<typeof schema.ItemSkin>;
51
- export type ItemSkinDocument = ItemSkin & Document;
52
-
53
- export type ItemTransmute = z.infer<typeof schema.ItemTransmute>;
54
- export type ItemTransmuteDocument = ItemTransmute & Document;
55
-
56
- export type Mappings = {
57
- Item: Model<ItemDocument>;
58
- ItemAttribute: Model<ItemAttributeDocument>;
59
- ItemMaterial: Model<ItemMaterialDocument>;
60
- ItemSet: Model<ItemSetDocument>;
61
- ItemSlot: Model<ItemSlotDocument>;
62
- ItemRarity: Model<ItemRarityDocument>;
63
- ItemRank: Model<ItemRankDocument>;
64
- ItemType: Model<ItemTypeDocument>;
65
- ItemSubType: Model<ItemSubTypeDocument>;
66
- ItemSpecificType: Model<ItemSpecificTypeDocument>;
67
- ItemAffix: Model<ItemAffixDocument>;
68
- ItemRecipe: Model<ItemRecipeDocument>;
69
- ItemSkin: Model<ItemSkinDocument>;
70
- ItemTransmute: Model<ItemTransmuteDocument>;
71
- };
72
-
73
- export type RouterInput = inferRouterInputs<Router>;
74
- export type RouterOutput = inferRouterOutputs<Router>;
@@ -1,5 +0,0 @@
1
- export * as Types from './job.types';
2
- export * as Models from './job.models';
3
- export * as Schemas from './job.schema';
4
- export * from './job.router';
5
- export * from './job.service';
@@ -1,14 +0,0 @@
1
- import * as mongo from '../../util/mongo';
2
- import type * as Types from './job.types';
3
-
4
- export const Job = mongo.createModel<Types.JobDocument>(
5
- 'Job',
6
- {
7
- mod: { type: String, required: true },
8
- startDate: { type: Date },
9
- expireDate: { type: Date },
10
- },
11
- {
12
- indexes: [{ applicationId: 1, mod: 1, key: 1 }],
13
- }
14
- );
@@ -1,44 +0,0 @@
1
- // job.router.ts
2
-
3
- import { z as zod } from 'zod';
4
- import { initTRPC, inferRouterInputs, inferRouterOutputs } from '@trpc/server';
5
- import { customErrorFormatter, hasRole } from '../../util/rpc';
6
- import type { RouterContext } from '../../types';
7
- import { Job } from './job.schema';
8
- import { Query } from '../../schema';
9
-
10
- export const z = zod;
11
- export const t = initTRPC.context<RouterContext>().create();
12
- export const router = t.router;
13
- export const procedure = t.procedure;
14
-
15
- export const createRouter = () =>
16
- router({
17
- getJob: procedure
18
- .use(hasRole('guest', t))
19
- .use(customErrorFormatter(t))
20
- .input(z.object({ query: Query }))
21
- .query(({ input, ctx }) => (ctx.app.service.Job.getJob as any)(input, ctx)),
22
-
23
- createJob: procedure
24
- .use(hasRole('admin', t))
25
- .use(customErrorFormatter(t))
26
- .input(z.object({ data: Job }))
27
- .mutation(({ input, ctx }) => (ctx.app.service.Job.createJob as any)(input, ctx)),
28
-
29
- updateJob: procedure
30
- .use(hasRole('admin', t))
31
- .use(customErrorFormatter(t))
32
- .input(z.object({ query: Query, data: Job.partial() }))
33
- .mutation(({ input, ctx }) => (ctx.app.service.Job.updateJob as any)(input, ctx)),
34
-
35
- updateMetrics: procedure
36
- .use(hasRole('admin', t))
37
- .use(customErrorFormatter(t))
38
- .input(z.any())
39
- .mutation(({ input, ctx }) => (ctx.app.service.Job.updateMetrics as any)(input, ctx)),
40
- });
41
-
42
- export type Router = ReturnType<typeof createRouter>;
43
- export type RouterInput = inferRouterInputs<Router>;
44
- export type RouterOutput = inferRouterOutputs<Router>;
@@ -1,9 +0,0 @@
1
- import { z, ObjectId, Entity } from '../../schema';
2
-
3
- export const Job = Entity.merge(
4
- z.object({
5
- mod: z.string(),
6
- startDate: z.date().optional(),
7
- expireDate: z.date().optional(),
8
- })
9
- );
@@ -1,243 +0,0 @@
1
- // job.service.ts
2
-
3
- import type { Job, RouterContext, RouterInput, RouterOutput } from './job.types';
4
- import { getFilter } from '../../util/api';
5
- import dayjs from 'dayjs';
6
-
7
- export class Service {
8
- async getJob(input: RouterInput['getJob'], ctx: RouterContext): Promise<RouterOutput['getJob']> {
9
- if (!input) throw new Error('Input should not be void');
10
- console.log('Job.Service.getJob', input.query);
11
-
12
- const filter = getFilter(input.query);
13
- const job = await ctx.app.model.Job.findOne(filter).lean().exec();
14
- if (!job) throw new Error('Job not found');
15
-
16
- return job as Job;
17
- }
18
-
19
- async createJob(input: RouterInput['createJob'], ctx: RouterContext): Promise<RouterOutput['createJob']> {
20
- if (!input) throw new Error('Input should not be void');
21
- console.log('Job.Service.createJob', input.data);
22
-
23
- const job = await ctx.app.model.Job.create(input.data);
24
- return job as Job;
25
- }
26
-
27
- async updateJob(input: RouterInput['updateJob'], ctx: RouterContext): Promise<RouterOutput['updateJob']> {
28
- if (!input) throw new Error('Input should not be void');
29
- console.log('Job.Service.updateJob', input.query, input.data);
30
-
31
- const filter = getFilter(input.query);
32
- const updatedJob = await ctx.app.model.Job.findOneAndUpdate(filter, input.data, { new: true }).lean().exec();
33
- if (!updatedJob) throw new Error('Job update failed');
34
-
35
- return updatedJob as Job;
36
- }
37
-
38
- async updateMetrics(input: RouterInput['updateMetrics'], ctx: RouterContext): Promise<RouterOutput['updateMetrics']> {
39
- if (!input) throw new Error('Input should not be void');
40
- console.log('Job.Service.updateMetrics');
41
-
42
- let latestRecord;
43
- let latestNumber = 1;
44
-
45
- const { Stat, Payment } = ctx.app.model;
46
-
47
- try {
48
- latestRecord = await Stat.findOne().sort({ number: -1 }).exec();
49
-
50
- if (latestRecord) {
51
- latestNumber = latestRecord.number;
52
-
53
- const dayAgo = dayjs().subtract(1, 'day');
54
-
55
- // Unset the latest record if it's older than one day, so a new one is created
56
- if (!dayjs(latestRecord.createdDate).isAfter(dayAgo)) {
57
- latestRecord = undefined;
58
- }
59
- }
60
- } catch (e) {
61
- console.log('Error getting latest stat record', e);
62
- }
63
-
64
- const totalPaymentsCount = await Payment.countDocuments().exec();
65
- const pendingPayments = await Payment.where({ status: { $in: ['Processing', 'Processed', 'Finalized'] } });
66
- const completedPayments = await Payment.where({ status: { $in: ['Completed'] } });
67
-
68
- const pendingTokens: any = {};
69
- const distributedTokens: any = {};
70
-
71
- for (const payment of pendingPayments) {
72
- for (const i in payment.meta.tokenKeys) {
73
- const key = payment.meta.tokenKeys[i];
74
- const amount = payment.meta.tokenAmounts[i];
75
-
76
- if (!pendingTokens[key]) pendingTokens[key] = 0;
77
-
78
- pendingTokens[key] += amount;
79
- }
80
- }
81
-
82
- for (const payment of completedPayments) {
83
- for (const i in payment.meta.tokenKeys) {
84
- const key = payment.meta.tokenKeys[i];
85
- const amount = payment.meta.tokenAmounts[i];
86
-
87
- if (!distributedTokens[key]) distributedTokens[key] = 0;
88
-
89
- distributedTokens[key] += amount;
90
- }
91
- }
92
-
93
- const pendingPaymentsCount = pendingPayments.length;
94
- const completedPaymentsCount = completedPayments.length;
95
-
96
- // const result = await ctx.app.model.Profile.aggregate([
97
- // {
98
- // $group: {
99
- // _id: null,
100
- // totalDoge: { $sum: "$meta.rewards.tokens.doge" },
101
- // totalPepe: { $sum: "$meta.rewards.tokens.pepe" },
102
- // }
103
- // }
104
- // ]);
105
- const pendingRewardTokens = await ctx.app.model.Profile.aggregate([
106
- {
107
- // Convert meta.rewards.tokens => { k, v } pairs
108
- $project: {
109
- tokens: { $objectToArray: '$meta.rewards.tokens' },
110
- },
111
- },
112
- {
113
- // Unwind each { k, v } into its own document
114
- $unwind: '$tokens',
115
- },
116
- {
117
- // Group by token name (k), summing up values (v)
118
- $group: {
119
- _id: '$tokens.k',
120
- total: { $sum: '$tokens.v' },
121
- },
122
- },
123
- {
124
- // Re-group into a single document with an array of { k, v }
125
- $group: {
126
- _id: null,
127
- tokens: {
128
- $push: {
129
- k: '$_id',
130
- v: '$total',
131
- },
132
- },
133
- },
134
- },
135
- {
136
- // Convert the array [ { k, v }, { k, v }, ... ] back into an object
137
- $project: {
138
- _id: 0,
139
- tokens: { $arrayToObject: '$tokens' },
140
- },
141
- },
142
- ]);
143
-
144
- const meta: any = {
145
- payments: {
146
- totalCount: totalPaymentsCount,
147
- pendingCount: pendingPaymentsCount,
148
- completedCount: completedPaymentsCount,
149
- status: {
150
- Processing: await Payment.where({ status: 'Processing' }).countDocuments().exec(),
151
- Processed: await Payment.where({ status: 'Processed' }).countDocuments().exec(),
152
- Finalized: await Payment.where({ status: 'Finalized' }).countDocuments().exec(),
153
- Completed: await Payment.where({ status: 'Completed' }).countDocuments().exec(),
154
- Voided: await Payment.where({ status: 'Voided' }).countDocuments().exec(),
155
- },
156
- pendingTokens,
157
- distributedTokens,
158
- },
159
- rewards: {
160
- pendingRewardTokens: pendingRewardTokens?.[0]?.tokens || {},
161
- },
162
- };
163
-
164
- if (latestRecord) {
165
- await Stat.updateOne(
166
- { _id: latestRecord.id },
167
- {
168
- ...latestRecord.toObject(),
169
- meta,
170
- }
171
- ).exec();
172
- } else {
173
- await Stat.create({
174
- number: latestNumber + 1,
175
- createdDate: new Date(),
176
- meta,
177
- });
178
- }
179
-
180
- // if (!input) throw new Error('Input should not be void');
181
- // console.log('Job.Service.updateMetrics');
182
-
183
- // let latestRecord;
184
- // let latestNumber = 1;
185
-
186
- // const { Stat, Interface, InterfaceComponent } = ctx.app.model;
187
-
188
- // try {
189
- // latestRecord = await Stat.findOne().sort({ number: -1 }).exec();
190
-
191
- // if (latestRecord) {
192
- // latestNumber = latestRecord.number;
193
-
194
- // const dayAgo = dayjs().subtract(1, 'day');
195
-
196
- // // Unset the latest record if it's older than one day, so a new one is created
197
- // if (!dayjs(latestRecord.createdDate).isAfter(dayAgo)) {
198
- // latestRecord = undefined;
199
- // }
200
- // }
201
- // } catch (e) {
202
- // console.log('Error getting latest stat record', e);
203
- // }
204
-
205
- // const TotalInterfaces = await Interface.countDocuments().exec();
206
- // const TotalInterfaceDrafted = await Interface.where({ status: 'Draft' }).countDocuments().exec();
207
- // const TotalInterfacePublished = await Interface.where({ status: 'Published' }).countDocuments().exec();
208
- // const TotalInterfacePaused = await Interface.where({ status: 'Paused' }).countDocuments().exec();
209
- // const TotalInterfaceFinished = await Interface.where({ status: 'Finished' }).countDocuments().exec();
210
- // const TotalInterfaceArchived = await Interface.where({ status: 'Archived' }).countDocuments().exec();
211
- // const TotalInterfaceSubmissions = 1; // Example static value, replace with actual logic if needed
212
- // const TotalInterfaceTemplates = await InterfaceComponent.countDocuments().exec();
213
-
214
- // const meta = {
215
- // TotalInterfaces,
216
- // TotalInterfaceDrafted,
217
- // TotalInterfacePublished,
218
- // TotalInterfacePaused,
219
- // TotalInterfaceArchived,
220
- // TotalInterfaceFinished,
221
- // TotalInterfaceSubmissions,
222
- // TotalInterfaceTemplates,
223
- // };
224
-
225
- // if (latestRecord) {
226
- // await Stat.updateOne(
227
- // { _id: latestRecord.id },
228
- // {
229
- // ...latestRecord.toObject(),
230
- // meta,
231
- // }
232
- // ).exec();
233
- // } else {
234
- // await Stat.create({
235
- // number: latestNumber + 1,
236
- // createdDate: new Date(),
237
- // meta,
238
- // });
239
- // }
240
-
241
- return { status: 1 };
242
- }
243
- }