Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Edit users #379

Merged
merged 6 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions backend/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date
"""
scalar DateTime

type EditAdminMembersObj {
birthday: DateTime!
email: String!
first_name: String!
id: Int!
last_name: String!
name: String!
newsletter: Boolean!
}

type EditAdminSettingsObj {
site_copyright: [TextLanguage!]!
site_description: [TextLanguage!]!
Expand Down Expand Up @@ -150,6 +160,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!): EditAdminMembersObj!
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;
}
25 changes: 25 additions & 0 deletions backend/src/plugins/core/admin/members/edit/dto/edit.obj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ObjectType, Field, Int } from "@nestjs/graphql";

@ObjectType()
export class EditAdminMembersObj {
@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;
}
21 changes: 21 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,21 @@
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";

Check warning on line 7 in backend/src/plugins/core/admin/members/edit/edit.resolver.ts

View workflow job for this annotation

GitHub Actions / install

There should be at least one empty line between import groups
import { EditAdminMembersObj } from "./dto/edit.obj";

Check warning on line 8 in backend/src/plugins/core/admin/members/edit/edit.resolver.ts

View workflow job for this annotation

GitHub Actions / install

`./dto/edit.obj` import should occur before import of `@/utils/guards/admin-auth.guard`

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

@Mutation(() => EditAdminMembersObj)
@UseGuards(AdminAuthGuards)
async admin__core_members__edit(
@Args() args: EditAdminMembersArgs
): Promise<EditAdminMembersObj> {
return this.service.edit(args);
}
}
54 changes: 54 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,54 @@
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";

Check warning on line 7 in backend/src/plugins/core/admin/members/edit/edit.service.ts

View workflow job for this annotation

GitHub Actions / install

There should be at least one empty line between import groups
import { NotFoundError } from "@vitnode/backend";

Check warning on line 8 in backend/src/plugins/core/admin/members/edit/edit.service.ts

View workflow job for this annotation

GitHub Actions / install

`@vitnode/backend` import should occur before import of `./dto/edit.args`

Check warning on line 8 in backend/src/plugins/core/admin/members/edit/edit.service.ts

View workflow job for this annotation

GitHub Actions / install

'@vitnode/backend' imported multiple times
import { AccessDeniedError } from "@vitnode/backend";

Check warning on line 9 in backend/src/plugins/core/admin/members/edit/edit.service.ts

View workflow job for this annotation

GitHub Actions / install

There should be at least one empty line between import groups

Check warning on line 9 in backend/src/plugins/core/admin/members/edit/edit.service.ts

View workflow job for this annotation

GitHub Actions / install

`@vitnode/backend` import should occur before import of `./dto/edit.args`
import { EditAdminMembersObj } from "./dto/edit.obj";

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

async edit({
id,
name,
email,
newsletter,
first_name,
last_name,
birthday
}: EditAdminMembersArgs): Promise<EditAdminMembersObj> {
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();

const update = 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))
.returning();

return update[0];
}
}
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
Loading