Skip to content

Commit

Permalink
feat: Edit users
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreSoftwareDeveloper committed Jun 12, 2024
1 parent 2935305 commit f7ad02c
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ type Mutation {
admin__core_languages__update(code: String!, file: Upload!): String!
admin__core_main_settings__edit(site_copyright: [TextLanguageInput!]!, site_description: [TextLanguageInput!]!, site_name: String!, site_short_name: String!): EditAdminSettingsObj!
admin__core_manifest_metadata__edit(background_color: String!, display: String!, start_url: String!, theme_color: String!): ShowAdminManifestMetadataObj!
admin__core_members__edit(birthday: DateTime!, email: String!, first_name: String!, id: Int!, last_name: String!, name: String!, newsletter: Boolean!): String!
admin__core_nav__change_position(id: Int!, index_to_move: Int!, parent_id: Int!): String!
admin__core_nav__create(description: [TextLanguageInput!]!, external: Boolean!, href: String!, icon: String, name: [TextLanguageInput!]!): ShowCoreNav!
admin__core_nav__delete(id: Int!): String!
Expand Down
25 changes: 25 additions & 0 deletions backend/src/plugins/core/admin/members/edit/dto/edit.args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ArgsType, Field, Int } from "@nestjs/graphql";

@ArgsType()
export class EditAdminMembersArgs {
@Field(() => Int)
id: number;

@Field(() => String)
name: string;

@Field(() => String)
email: string;

@Field(() => Boolean)
newsletter: boolean;

@Field(() => String)
first_name: string;

@Field(() => String)
last_name: string;

@Field(() => Date)
birthday: Date;
}
20 changes: 20 additions & 0 deletions backend/src/plugins/core/admin/members/edit/edit.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Args, Mutation, Resolver } from "@nestjs/graphql";
import { UseGuards } from "@nestjs/common";

import { EditAdminMembersService } from "./edit.service";
import { EditAdminMembersArgs } from "./dto/edit.args";

import { AdminAuthGuards } from "@/utils/guards/admin-auth.guard";

@Resolver()
export class EditAdminMembersResolver {
constructor(private readonly service: EditAdminMembersService) {}

@Mutation(() => String)
@UseGuards(AdminAuthGuards)
async admin__core_members__edit(
@Args() args: EditAdminMembersArgs
): Promise<string> {
return this.service.edit(args);
}
}
52 changes: 52 additions & 0 deletions backend/src/plugins/core/admin/members/edit/edit.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Injectable } from "@nestjs/common";
import { eq } from "drizzle-orm";

import { EditAdminMembersArgs } from "./dto/edit.args";

import { core_users } from "@/plugins/core/admin/database/schema/users";
import { DatabaseService } from "@/database/database.service";
import { NotFoundError } from "@/utils/errors/not-found-error";
import { AccessDeniedError } from "@/utils/errors/access-denied-error";

@Injectable()
export class EditAdminMembersService {
constructor(private readonly databaseService: DatabaseService) {}

async edit({
id,
name,
email,
newsletter,
first_name,
last_name,
birthday
}: EditAdminMembersArgs): Promise<string> {
const user = await this.databaseService.db.query.core_users.findFirst({
where: eq(core_users.id, id)
});

if (!user) throw new NotFoundError("User");

const admin =
await this.databaseService.db.query.core_admin_permissions.findFirst({
where: (table, { or, eq }) =>
or(eq(table.user_id, user.id), eq(table.group_id, user.group_id))
});

if (!admin) throw new AccessDeniedError();

await this.databaseService.db
.update(core_users)
.set({
name: name,
email: email,
newsletter: newsletter,
first_name: first_name,
last_name: last_name,
birthday: birthday
})
.where(eq(core_users.id, id));

return "Success!";
}
}
6 changes: 5 additions & 1 deletion backend/src/plugins/core/admin/members/members.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import { ShowAdminMembersResolver } from "./show/show.resolver";
import { ShowAdminMembersService } from "./show/show.service";
import { StatsAdminMembersResolver } from "./stats/stats.resolver";
import { StatsAdminMembersService } from "./stats/stats.service";
import { EditAdminMembersResolver } from "./edit/edit.resolver";
import { EditAdminMembersService } from "./edit/edit.service";

@Module({
providers: [
ShowAdminMembersResolver,
ShowAdminMembersService,
StatsAdminMembersResolver,
StatsAdminMembersService
StatsAdminMembersService,
EditAdminMembersResolver,
EditAdminMembersService
]
})
export class AdminMembersModule {}
2 changes: 1 addition & 1 deletion backend/src/plugins/core/members/delete/delete.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class DeleteCoreMembersService {
or(eq(table.user_id, user.id), eq(table.group_id, user.group_id))
});

if (admin) throw new AccessDeniedError();
if (admin) throw new AccessDeniedError(); //Protects against deletion users with admin permisssions

await this.databaseService.db
.delete(core_users)
Expand Down

0 comments on commit f7ad02c

Please sign in to comment.