Skip to content

Commit 9954cae

Browse files
committed
del effect
1 parent 6de4228 commit 9954cae

File tree

3 files changed

+95
-124
lines changed

3 files changed

+95
-124
lines changed

bun.lock

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,14 +468,12 @@
468468
"@databuddy/redis": "workspace:*",
469469
"@databuddy/shared": "workspace:*",
470470
"@databuddy/validation": "workspace:*",
471-
"@logtail/edge": "^0.5.7",
472471
"@opentelemetry/api": "^1.9.0",
473472
"@orpc/otel": "^1.11.2",
474473
"@orpc/server": "^1.11.2",
475474
"ai": "^5.0.93",
476475
"autumn-js": "^0.0.101",
477476
"drizzle-orm": "^0.44.7",
478-
"effect": "^3.19.3",
479477
"nanoid": "^5.1.6",
480478
"pg": "^8.16.3",
481479
"superjson": "^2.2.5",

packages/rpc/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
"@databuddy/redis": "workspace:*",
1212
"@databuddy/shared": "workspace:*",
1313
"@databuddy/validation": "workspace:*",
14-
"@logtail/edge": "^0.5.7",
1514
"@opentelemetry/api": "^1.9.0",
1615
"@orpc/otel": "^1.11.2",
1716
"@orpc/server": "^1.11.2",
1817
"ai": "^5.0.93",
1918
"autumn-js": "^0.0.101",
2019
"drizzle-orm": "^0.44.7",
21-
"effect": "^3.19.3",
2220
"nanoid": "^5.1.6",
2321
"pg": "^8.16.3",
2422
"superjson": "^2.2.5",

packages/rpc/src/routers/websites.ts

Lines changed: 95 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ import {
1111
updateWebsiteSchema,
1212
} from "@databuddy/validation";
1313
import { ORPCError } from "@orpc/server";
14-
import { Effect, pipe } from "effect";
1514
import { z } from "zod";
1615
import { protectedProcedure, publicProcedure } from "../orpc";
1716
import {
1817
buildWebsiteFilter,
1918
DuplicateDomainError,
2019
ValidationError,
2120
type Website,
22-
type WebsiteError,
2321
WebsiteNotFoundError,
2422
WebsiteService,
2523
} from "../services/website-service";
@@ -255,25 +253,21 @@ export const websitesRouter = {
255253
organizationId: input.organizationId,
256254
};
257255

258-
const result = await pipe(
259-
new WebsiteService(context.db).createWebsite(serviceInput),
260-
Effect.mapError((error: WebsiteError) => {
261-
if (error instanceof ValidationError) {
262-
return new ORPCError("BAD_REQUEST", {
263-
message: error.message,
264-
});
265-
}
266-
if (error instanceof DuplicateDomainError) {
267-
return new ORPCError("CONFLICT", { message: error.message });
268-
}
269-
return new ORPCError("INTERNAL_SERVER_ERROR", {
256+
try {
257+
return await new WebsiteService(context.db).createWebsite(serviceInput);
258+
} catch (error) {
259+
if (error instanceof ValidationError) {
260+
throw new ORPCError("BAD_REQUEST", {
270261
message: error.message,
271262
});
272-
}),
273-
Effect.runPromise
274-
);
275-
276-
return result;
263+
}
264+
if (error instanceof DuplicateDomainError) {
265+
throw new ORPCError("CONFLICT", { message: error.message });
266+
}
267+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
268+
message: error instanceof Error ? error.message : String(error),
269+
});
270+
}
277271
}),
278272

279273
update: protectedProcedure
@@ -290,31 +284,30 @@ export const websitesRouter = {
290284
domain: input.domain,
291285
};
292286

293-
const updatedWebsite: Website = await pipe(
294-
new WebsiteService(context.db).updateWebsite(
287+
let updatedWebsite: Website;
288+
try {
289+
updatedWebsite = await new WebsiteService(context.db).updateWebsite(
295290
input.id,
296291
serviceInput,
297292
context.user.id,
298293
websiteToUpdate.organizationId ?? undefined
299-
),
300-
Effect.mapError((error: WebsiteError) => {
301-
if (error instanceof ValidationError) {
302-
return new ORPCError("BAD_REQUEST", {
303-
message: error.message,
304-
});
305-
}
306-
if (error instanceof DuplicateDomainError) {
307-
return new ORPCError("CONFLICT", { message: error.message });
308-
}
309-
if (error instanceof WebsiteNotFoundError) {
310-
return new ORPCError("NOT_FOUND", { message: error.message });
311-
}
312-
return new ORPCError("INTERNAL_SERVER_ERROR", {
294+
);
295+
} catch (error) {
296+
if (error instanceof ValidationError) {
297+
throw new ORPCError("BAD_REQUEST", {
313298
message: error.message,
314299
});
315-
}),
316-
Effect.runPromise
317-
);
300+
}
301+
if (error instanceof DuplicateDomainError) {
302+
throw new ORPCError("CONFLICT", { message: error.message });
303+
}
304+
if (error instanceof WebsiteNotFoundError) {
305+
throw new ORPCError("NOT_FOUND", { message: error.message });
306+
}
307+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
308+
message: error instanceof Error ? error.message : String(error),
309+
});
310+
}
318311

319312
const changes: string[] = [];
320313
if (input.name !== websiteToUpdate.name) {
@@ -327,10 +320,10 @@ export const websitesRouter = {
327320
}
328321

329322
if (changes.length > 0) {
330-
logger.info("Website Updated", changes.join(", "), {
323+
logger.info({
331324
websiteId: updatedWebsite.id,
332325
userId: context.user.id,
333-
});
326+
}, `Website Updated: ${changes.join(", ")}`);
334327
}
335328

336329
return updatedWebsite;
@@ -341,31 +334,30 @@ export const websitesRouter = {
341334
.handler(async ({ context, input }) => {
342335
const website = await authorizeWebsiteAccess(context, input.id, "update");
343336

344-
const updatedWebsite = await pipe(
345-
new WebsiteService(context.db).toggleWebsitePublic(
337+
let updatedWebsite: Website;
338+
try {
339+
updatedWebsite = await new WebsiteService(context.db).toggleWebsitePublic(
346340
input.id,
347341
input.isPublic,
348342
context.user.id
349-
),
350-
Effect.mapError((error: WebsiteError) => {
351-
if (error instanceof WebsiteNotFoundError) {
352-
return new ORPCError("NOT_FOUND", { message: error.message });
353-
}
354-
return new ORPCError("INTERNAL_SERVER_ERROR", {
355-
message: error.message,
356-
});
357-
}),
358-
Effect.runPromise
359-
);
343+
);
344+
} catch (error) {
345+
if (error instanceof WebsiteNotFoundError) {
346+
throw new ORPCError("NOT_FOUND", { message: error.message });
347+
}
348+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
349+
message: error instanceof Error ? error.message : String(error),
350+
});
351+
}
360352

361353
logger.info(
362-
"Website Privacy Updated",
363-
`${website.domain} is now ${input.isPublic ? "public" : "private"}`,
364354
{
365355
websiteId: input.id,
366356
isPublic: input.isPublic,
367357
userId: context.user.id,
368-
}
358+
event: "Website Privacy Updated"
359+
},
360+
`${website.domain} is now ${input.isPublic ? "public" : "private"}`
369361
);
370362

371363
return updatedWebsite;
@@ -380,26 +372,26 @@ export const websitesRouter = {
380372
"delete"
381373
);
382374

383-
await pipe(
384-
new WebsiteService(context.db).deleteWebsite(input.id, context.user.id),
385-
Effect.mapError(
386-
(error: WebsiteError) =>
387-
new ORPCError("INTERNAL_SERVER_ERROR", {
388-
message: error.message,
389-
})
390-
),
391-
Effect.runPromise
392-
);
375+
try {
376+
await new WebsiteService(context.db).deleteWebsite(
377+
input.id,
378+
context.user.id
379+
);
380+
} catch (error) {
381+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
382+
message: error instanceof Error ? error.message : String(error),
383+
});
384+
}
393385

394-
logger.warning(
395-
"Website Deleted",
396-
`Website "${websiteToDelete.name}" with domain "${websiteToDelete.domain}" was deleted`,
386+
logger.warn(
397387
{
398388
websiteId: websiteToDelete.id,
399389
websiteName: websiteToDelete.name,
400390
domain: websiteToDelete.domain,
401391
userId: context.user.id,
402-
}
392+
event: "Website Deleted"
393+
},
394+
`Website "${websiteToDelete.name}" with domain "${websiteToDelete.domain}" was deleted`
403395
);
404396

405397
return { success: true };
@@ -425,24 +417,20 @@ export const websitesRouter = {
425417
}
426418
}
427419

428-
const result = await pipe(
429-
new WebsiteService(context.db).transferWebsite(
420+
try {
421+
return await new WebsiteService(context.db).transferWebsite(
430422
input.websiteId,
431423
input.organizationId ?? null,
432424
context.user.id
433-
),
434-
Effect.mapError((error: WebsiteError) => {
435-
if (error instanceof WebsiteNotFoundError) {
436-
return new ORPCError("NOT_FOUND", { message: error.message });
437-
}
438-
return new ORPCError("INTERNAL_SERVER_ERROR", {
439-
message: error.message,
440-
});
441-
}),
442-
Effect.runPromise
443-
);
444-
445-
return result;
425+
);
426+
} catch (error) {
427+
if (error instanceof WebsiteNotFoundError) {
428+
throw new ORPCError("NOT_FOUND", { message: error.message });
429+
}
430+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
431+
message: error instanceof Error ? error.message : String(error),
432+
});
433+
}
446434
}),
447435

448436
transferToOrganization: protectedProcedure
@@ -464,44 +452,34 @@ export const websitesRouter = {
464452
});
465453
}
466454

467-
const result = await pipe(
468-
new WebsiteService(context.db).transferWebsiteToOrganization(
455+
try {
456+
return await new WebsiteService(context.db).transferWebsiteToOrganization(
469457
input.websiteId,
470458
input.targetOrganizationId,
471459
context.user.id
472-
),
473-
Effect.mapError((error: WebsiteError) => {
474-
if (error instanceof WebsiteNotFoundError) {
475-
return new ORPCError("NOT_FOUND", { message: error.message });
476-
}
477-
return new ORPCError("INTERNAL_SERVER_ERROR", {
478-
message: error.message,
479-
});
480-
}),
481-
Effect.runPromise
482-
);
483-
484-
return result;
460+
);
461+
} catch (error) {
462+
if (error instanceof WebsiteNotFoundError) {
463+
throw new ORPCError("NOT_FOUND", { message: error.message });
464+
}
465+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
466+
message: error instanceof Error ? error.message : String(error),
467+
});
468+
}
485469
}),
486470

487471
invalidateCaches: protectedProcedure
488472
.input(z.object({ websiteId: z.string() }))
489473
.handler(async ({ context, input }) => {
490474
await authorizeWebsiteAccess(context, input.websiteId, "update");
491475

492-
await pipe(
493-
Effect.tryPromise({
494-
try: () => invalidateWebsiteCaches(input.websiteId, context.user.id),
495-
catch: () => new Error("Failed to invalidate caches"),
496-
}),
497-
Effect.mapError(
498-
() =>
499-
new ORPCError("INTERNAL_SERVER_ERROR", {
500-
message: "Failed to invalidate caches",
501-
})
502-
),
503-
Effect.runPromise
504-
);
476+
try {
477+
await invalidateWebsiteCaches(input.websiteId, context.user.id);
478+
} catch {
479+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
480+
message: "Failed to invalidate caches",
481+
});
482+
}
505483

506484
return { success: true };
507485
}),
@@ -530,11 +508,9 @@ export const websitesRouter = {
530508
hasTrackingEvents = (trackingCheckResult[0]?.count ?? 0) > 0;
531509
} catch (error) {
532510
logger.error(
533-
"Error checking tracking events:",
534-
error instanceof Error ? error.message : String(error),
535-
{ websiteId: input.websiteId }
511+
{ websiteId: input.websiteId },
512+
`Error checking tracking events: ${error instanceof Error ? error.message : String(error)}`
536513
);
537-
// Default to false if query fails
538514
hasTrackingEvents = false;
539515
}
540516

@@ -544,9 +520,8 @@ export const websitesRouter = {
544520
};
545521
} catch (error) {
546522
logger.error(
547-
"Error in isTrackingSetup:",
548-
error instanceof Error ? error.message : String(error),
549-
{ websiteId: input.websiteId }
523+
{ websiteId: input.websiteId },
524+
`Error in isTrackingSetup: ${error instanceof Error ? error.message : String(error)}`
550525
);
551526
throw error;
552527
}

0 commit comments

Comments
 (0)