Skip to content

Commit

Permalink
include isVerified and authorization checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dhakalaashish committed Sep 8, 2023
1 parent 1847b83 commit 801badb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions apps/server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ model GeoEventProvider {
clientId String // LANDSAT_NRT
fetchFrequency Int?
isActive Boolean
isVerified Boolean
lastRun DateTime?
config Json
userId String?
Expand Down
30 changes: 26 additions & 4 deletions apps/server/src/server/api/routers/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ export const alertRouter = createTRPCRouter({
});
}
}),


// TODO: Make sure that the siteId must belong to the clientApiKey!
// TODO: We need to check if the geoEventProvider is verified or enabled or not!
create: protectedProcedure
.input(createAlertSchema)
.mutation(async ({ ctx, input }) => {
Expand Down Expand Up @@ -219,12 +221,32 @@ export const alertRouter = createTRPCRouter({
});
}

// Get site from the database using siteId; if not found, throw an error
const site = await ctx.prisma.site.findUnique({ where: { id: siteId } });
if(!provider.isVerified){
throw new TRPCError({
code: "METHOD_NOT_SUPPORTED",
message: `GeoEventProvider is not verified. Verify it first to create alerts.`,
});
}

// Find the userId associated with the provider
// Since the provider is either found by using the user's authorization headers, or by using the clientApiKey
// This ensures that, there is no difference between a user accessing their own provider,
// or someone else accessing the provider with the clientApiKey (which acts as a password for the provider)
// Then, we can find the provider.userId for that provider.
const providerUserId = provider.userId ? provider.userId : ""

// Get site from the database using siteId and providerUserId; if not found, throw an error
const site = await ctx.prisma.site.findUnique({
where: {
id: siteId,
userId: providerUserId,
}
});
if (!site) {
throw new TRPCError({
code: "NOT_FOUND",
message: `Site Not Found`,
message: `Site Not Found.`,
// Either the site does not exist, or not authorized to access that site.
});
}

Expand Down

0 comments on commit 801badb

Please sign in to comment.