Skip to content

Commit 602597a

Browse files
committed
jobqueue and stauts service
1 parent 3bdfe55 commit 602597a

File tree

8 files changed

+270
-124
lines changed

8 files changed

+270
-124
lines changed

server/src/init/services.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import { MeService } from "@/services/index.js";
3636
import { MongoMonitorRepository } from "@/repositories/index.js";
3737

3838
export const initServices = async () => {
39+
const monitorRepository = new MongoMonitorRepository();
40+
3941
const checkService = new CheckService();
4042
const inviteService = new InviteService();
4143
const maintenanceService = new MaintenanceService();
@@ -46,7 +48,7 @@ export const initServices = async () => {
4648
const slackService = new SlackService();
4749
const webhookService = new WebhookService();
4850
const networkService = new NetworkService(got);
49-
const statusService = new StatusService();
51+
const statusService = new StatusService(monitorRepository);
5052
const settingsService = new SettingsService();
5153
const stripeService = new StripeService();
5254
const billingService = new BillingService();
@@ -68,11 +70,10 @@ export const initServices = async () => {
6870
maintenanceService,
6971
statsAggregationService
7072
);
71-
const jobQueue = await JobQueue.create(jobGenerator);
73+
const jobQueue = await JobQueue.create(jobGenerator, monitorRepository);
7274
const entitlementsProvider = EntitlementsFactory.create();
7375
const authService = new AuthService(jobQueue, entitlementsProvider);
7476
const meService = new MeService(entitlementsProvider);
75-
const monitorRepository = new MongoMonitorRepository();
7677
const monitorService = new MonitorService(jobQueue, monitorRepository);
7778
const queueService = new QueueService(jobQueue);
7879
const teamService = new TeamService(jobQueue);

server/src/repositories/monitors/IMonitorRepoistory.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ import type {
44
MonitorStatus,
55
} from "@/types/domain/monitor.js";
66

7+
export type UpdateMonitorPatch = Partial<
8+
Pick<
9+
Monitor,
10+
| "name"
11+
| "secret"
12+
| "interval"
13+
| "rejectUnauthorized"
14+
| "n"
15+
| "thresholds"
16+
| "notificationChannels"
17+
| "status"
18+
>
19+
>;
20+
721
export interface TeamQueryConfig {
822
search: string;
923
sortField: string;
@@ -15,7 +29,16 @@ export interface TeamQueryConfig {
1529
}
1630

1731
export interface IMonitorRepository {
32+
// Create
33+
create(
34+
orgId: string,
35+
userId: string,
36+
currentTeamId: string,
37+
monitorData: Monitor
38+
): Promise<Monitor>;
39+
1840
// Single fetch
41+
findAll(): Promise<Monitor[]>;
1942
findById(monitorId: string, teamId: string): Promise<Monitor | null>;
2043

2144
// Collection fetches
@@ -34,8 +57,23 @@ export interface IMonitorRepository {
3457
findByOrgId(orgId: string): Promise<Monitor[]>;
3558

3659
// Deletions
37-
deleteById(id: string): Promise<boolean>;
38-
deleteByOrgId(orgId: string): Promise<number>;
39-
}
60+
deleteById(monitorId: string, teamId: string): Promise<boolean>;
61+
deleteByOrgId(orgId: string): Promise<boolean>;
62+
63+
// Updates
64+
togglePauseById(
65+
monitorId: string,
66+
teamId: string,
67+
userId: string
68+
): Promise<Monitor | null>;
69+
70+
updateById(
71+
monitorId: string,
72+
teamId: string,
73+
userId: string,
74+
patch: UpdateMonitorPatch
75+
): Promise<Monitor | null>;
4076

41-
export default IMonitorRepository;
77+
// Counts
78+
countByOrgId(orgId: string): Promise<number>;
79+
}

server/src/repositories/monitors/MongoMonitorRepository.ts

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ class MongoMonitorRepository implements IMonitorRepository {
3030
};
3131
};
3232

33+
// Create
34+
create = async (
35+
orgId: string,
36+
userId: string,
37+
currentTeamId: string,
38+
monitorData: MonitorEntity
39+
) => {
40+
const monitorLiteral: Partial<IMonitor> = {
41+
...monitorData,
42+
orgId: new mongoose.Types.ObjectId(orgId),
43+
teamId: new mongoose.Types.ObjectId(currentTeamId),
44+
createdBy: new mongoose.Types.ObjectId(userId),
45+
updatedBy: new mongoose.Types.ObjectId(userId),
46+
notificationChannels: monitorData.notificationChannels?.map(
47+
(id) => new mongoose.Types.ObjectId(id)
48+
),
49+
};
50+
const monitor = await Monitor.create(monitorLiteral);
51+
return this.toEntity(monitor);
52+
};
53+
3354
// Single fetch
3455
findById = async (monitorId: string, teamId: string) => {
3556
const monitor = await Monitor.findOne({ _id: monitorId, teamId });
@@ -38,6 +59,11 @@ class MongoMonitorRepository implements IMonitorRepository {
3859
};
3960

4061
// Collection fetches
62+
findAll = async () => {
63+
const monitors = await Monitor.find();
64+
return monitors.map(this.toEntity);
65+
};
66+
4167
findByTeamId = async (teamId: string) => {
4268
const monitors = await Monitor.find({ teamId });
4369
return monitors.map(this.toEntity);
@@ -81,8 +107,10 @@ class MongoMonitorRepository implements IMonitorRepository {
81107

82108
return monitors.map(this.toEntity);
83109
};
110+
84111
findByOrgId = async (orgId: string) => {
85-
return [];
112+
const monitors = await Monitor.find({ orgId });
113+
return monitors.map(this.toEntity);
86114
};
87115

88116
findMonitorCountsByTeamId = async (teamId: string) => {
@@ -115,12 +143,69 @@ class MongoMonitorRepository implements IMonitorRepository {
115143
};
116144

117145
// Deletions
118-
deleteById = async (id: string) => {
119-
return false;
146+
deleteById = async (monitorId: string, teamId: string) => {
147+
await Monitor.deleteOne({ _id: monitorId, teamId });
148+
return true;
120149
};
121150
deleteByOrgId = async (orgId: string) => {
122-
return 0;
151+
const deleted = await Monitor.deleteMany({ orgId });
152+
return deleted.acknowledged;
123153
};
154+
155+
togglePauseById = async (
156+
monitorId: string,
157+
teamId: string,
158+
userId: string
159+
) => {
160+
const monitor = await Monitor.findOneAndUpdate(
161+
{ _id: monitorId, teamId },
162+
[
163+
{
164+
$set: {
165+
status: {
166+
$cond: {
167+
if: { $eq: ["$status", "paused"] },
168+
then: "resuming",
169+
else: "paused",
170+
},
171+
},
172+
updatedBy: userId,
173+
updatedAt: new Date(),
174+
},
175+
},
176+
],
177+
{ new: true }
178+
);
179+
if (!monitor) return null;
180+
return this.toEntity(monitor);
181+
};
182+
183+
// Update
184+
updateById = async (
185+
monitorId: string,
186+
teamId: string,
187+
userId: string,
188+
patch: Partial<MonitorEntity>
189+
) => {
190+
const updatedMonitor = await Monitor.findOneAndUpdate(
191+
{ _id: monitorId, teamId },
192+
{
193+
$set: {
194+
...patch,
195+
updatedAt: new Date(),
196+
updatedBy: userId,
197+
},
198+
},
199+
{ new: true, runValidators: true }
200+
);
201+
if (!updatedMonitor) return null;
202+
return this.toEntity(updatedMonitor);
203+
};
204+
205+
// Counts
206+
countByOrgId(orgId: string): Promise<number> {
207+
return Monitor.countDocuments({ orgId });
208+
}
124209
}
125210

126211
export default MongoMonitorRepository;

0 commit comments

Comments
 (0)