@hedhog/admin 0.12.1 → 0.12.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{hedhog.yaml → bkp.hedhog.yaml} +207 -9
- package/dist/auth/auth.controller.d.ts +1 -10
- package/dist/auth/auth.controller.d.ts.map +1 -1
- package/dist/auth/auth.service.d.ts +1 -10
- package/dist/auth/auth.service.d.ts.map +1 -1
- package/dist/auth/auth.service.js.map +1 -1
- package/dist/auth/auth.service.spec.d.ts +0 -1
- package/dist/auth/auth.service.spec.js +171 -161
- package/dist/auth/auth.service.spec.js.map +1 -1
- package/dist/menu/menu.controller.d.ts +6 -33
- package/dist/menu/menu.controller.d.ts.map +1 -1
- package/dist/menu/menu.service.d.ts +6 -33
- package/dist/menu/menu.service.d.ts.map +1 -1
- package/dist/menu/menu.service.js.map +1 -1
- package/dist/role/guards/role.guard.js.map +1 -1
- package/dist/role/role.controller.d.ts +5 -5
- package/dist/role/role.service.d.ts +5 -5
- package/dist/role/role.service.js.map +1 -1
- package/dist/route/route.controller.d.ts +6 -27
- package/dist/route/route.controller.d.ts.map +1 -1
- package/dist/route/route.service.d.ts +6 -27
- package/dist/route/route.service.d.ts.map +1 -1
- package/dist/route/route.service.js.map +1 -1
- package/dist/screen/screen.controller.d.ts +6 -24
- package/dist/screen/screen.controller.d.ts.map +1 -1
- package/dist/screen/screen.service.d.ts +6 -24
- package/dist/screen/screen.service.d.ts.map +1 -1
- package/dist/screen/screen.service.js.map +1 -1
- package/dist/user/user.controller.d.ts +5 -32
- package/dist/user/user.controller.d.ts.map +1 -1
- package/dist/user/user.service.d.ts +5 -32
- package/dist/user/user.service.d.ts.map +1 -1
- package/dist/user/user.service.js.map +1 -1
- package/dist/user/user.service.spec.d.ts +0 -1
- package/dist/user/user.service.spec.js +259 -250
- package/dist/user/user.service.spec.js.map +1 -1
- package/frontend/menu/components/create-panel.tsx.ejs +55 -0
- package/frontend/menu/components/update-panel.tsx.ejs +67 -0
- package/frontend/menu/locales/en/admin.menu.json +11 -0
- package/frontend/menu/locales/pt/admin.menu.json +11 -0
- package/frontend/menu/react-query/handlers.ts.ejs +28 -0
- package/frontend/menu/react-query/requests.ts.ejs +56 -0
- package/frontend/menu-locale/locales/en/admin.menu-locale.json +11 -0
- package/frontend/menu-locale/locales/pt/admin.menu-locale.json +11 -0
- package/frontend/menu-screen/locales/en/admin.menu-screen.json +11 -0
- package/frontend/menu-screen/locales/pt/admin.menu-screen.json +11 -0
- package/frontend/multifactor/components/create-panel.tsx.ejs +55 -0
- package/frontend/multifactor/components/update-panel.tsx.ejs +70 -0
- package/frontend/multifactor/locales/en/admin.multifactor.json +11 -0
- package/frontend/multifactor/locales/pt/admin.multifactor.json +11 -0
- package/frontend/multifactor/react-query/handlers.ts.ejs +28 -0
- package/frontend/multifactor/react-query/requests.ts.ejs +59 -0
- package/frontend/multifactor-locale/locales/en/admin.multifactor-locale.json +11 -0
- package/frontend/multifactor-locale/locales/pt/admin.multifactor-locale.json +11 -0
- package/frontend/screen/components/create-panel.tsx.ejs +55 -0
- package/frontend/screen/components/update-panel.tsx.ejs +67 -0
- package/frontend/screen/locales/en/admin.screen.json +11 -0
- package/frontend/screen/locales/pt/admin.screen.json +11 -0
- package/frontend/screen/react-query/handlers.ts.ejs +28 -0
- package/frontend/screen/react-query/requests.ts.ejs +56 -0
- package/frontend/screen-locale/locales/en/admin.screen-locale.json +11 -0
- package/frontend/screen-locale/locales/pt/admin.screen-locale.json +11 -0
- package/frontend/translation/components/create-panel.tsx.ejs +52 -0
- package/frontend/translation/components/update-panel.tsx.ejs +67 -0
- package/frontend/translation/fields/en.json +1 -0
- package/frontend/translation/fields/pt.json +1 -0
- package/frontend/translation/locales/en/admin.translation.json +11 -0
- package/frontend/translation/locales/pt/admin.translation.json +11 -0
- package/frontend/translation/modules/en.json +1 -0
- package/frontend/translation/modules/pt.json +1 -0
- package/frontend/translation/react-query/handlers.ts.ejs +28 -0
- package/frontend/translation/react-query/requests.ts.ejs +58 -0
- package/frontend/translation-namespace/components/create-panel.tsx.ejs +53 -0
- package/frontend/translation-namespace/components/update-panel.tsx.ejs +70 -0
- package/frontend/translation-namespace/locales/en/admin.translation-namespace.json +11 -0
- package/frontend/translation-namespace/locales/pt/admin.translation-namespace.json +11 -0
- package/frontend/translation-namespace/react-query/handlers.ts.ejs +28 -0
- package/frontend/translation-namespace/react-query/requests.ts.ejs +60 -0
- package/frontend/user/components/create-panel.tsx.ejs +52 -0
- package/frontend/user/components/update-panel.tsx.ejs +64 -0
- package/frontend/user/locales/en/admin.user.json +11 -0
- package/frontend/user/locales/pt/admin.user.json +11 -0
- package/frontend/user/react-query/handlers.ts.ejs +28 -0
- package/frontend/user/react-query/requests.ts.ejs +55 -0
- package/package.json +43 -43
- package/src/auth/auth.service.spec.ts +196 -196
- package/src/auth/auth.service.ts +10 -10
- package/src/menu/menu.service.ts +16 -16
- package/src/role/guards/role.guard.ts +2 -2
- package/src/role/role.service.ts +17 -17
- package/src/route/route.service.ts +13 -13
- package/src/screen/screen.service.ts +14 -14
- package/src/user/user.service.spec.ts +294 -294
- package/src/user/user.service.ts +10 -10
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import FormPanel, { FormPanelRef } from "@/components/panels/form-panel";
|
|
2
|
+
import { Overlay } from "@/components/custom/overlay";
|
|
3
|
+
import { TabPanel } from "@/components/panels/tab-panel";
|
|
4
|
+
import { useUserGet, useUserUpdate } from "@/features/admin/user";
|
|
5
|
+
import useEffectAfterFirstUpdate from "@/hooks/use-effect-after-first-update";
|
|
6
|
+
import { User } from "@/types/models";
|
|
7
|
+
import { useState, forwardRef, useImperativeHandle, useRef } from "react";
|
|
8
|
+
import { useTranslation } from "react-i18next";
|
|
9
|
+
import { IconEdit, IconPlus, IconTrash } from "@tabler/icons-react";
|
|
10
|
+
|
|
11
|
+
export type UserUpdatePanelProps = {
|
|
12
|
+
data: User;
|
|
13
|
+
onUpdated?: (data: User) => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const UserUpdatePanel = forwardRef(
|
|
17
|
+
({ data, onUpdated }: UserUpdatePanelProps, ref) => {
|
|
18
|
+
const { t } = useTranslation(["actions", "fields", "translations"]);
|
|
19
|
+
const { data: item, isLoading } = useUserGet(data.id as number);
|
|
20
|
+
const { mutate: userUpdate } = useUserUpdate();
|
|
21
|
+
const formRef = useRef<FormPanelRef>(null);
|
|
22
|
+
|
|
23
|
+
useEffectAfterFirstUpdate(() => {
|
|
24
|
+
if (item && formRef.current) {
|
|
25
|
+
formRef.current.setValuesFromItem(item);
|
|
26
|
+
}
|
|
27
|
+
}, [item]);
|
|
28
|
+
|
|
29
|
+
useImperativeHandle(ref, () => ({}));
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<TabPanel
|
|
33
|
+
activeTabIndex={0}
|
|
34
|
+
tabs={[
|
|
35
|
+
{
|
|
36
|
+
title: t("details", { ns: "actions" }),
|
|
37
|
+
children: (
|
|
38
|
+
<Overlay loading={isLoading}>
|
|
39
|
+
<FormPanel
|
|
40
|
+
ref={formRef}
|
|
41
|
+
fields={[]}
|
|
42
|
+
button={{ text: t("save", { ns: "actions" }) }}
|
|
43
|
+
onSubmit={(data) => {
|
|
44
|
+
userUpdate({
|
|
45
|
+
id: data.id,
|
|
46
|
+
data,
|
|
47
|
+
});
|
|
48
|
+
if (typeof onUpdated === "function") {
|
|
49
|
+
onUpdated(data);
|
|
50
|
+
}
|
|
51
|
+
}}
|
|
52
|
+
/>
|
|
53
|
+
</Overlay>
|
|
54
|
+
),
|
|
55
|
+
},
|
|
56
|
+
]}
|
|
57
|
+
/>
|
|
58
|
+
);
|
|
59
|
+
},
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
UserUpdatePanel.displayName = "UserUpdatePanel";
|
|
63
|
+
|
|
64
|
+
export default UserUpdatePanel;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"create": "Create user",
|
|
3
|
+
"createText": "Fill the user informations.",
|
|
4
|
+
"createTooltip": "Create new user",
|
|
5
|
+
"delete": "Delete user",
|
|
6
|
+
"deleteText": "Are you sure to delete these user?",
|
|
7
|
+
"deleteTooltip": "Delete the selected user",
|
|
8
|
+
"edit": "Edit user",
|
|
9
|
+
"editText": "View and edit user information.",
|
|
10
|
+
"editTooltip": "Edit the selected user"
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"create": "Criar usuário",
|
|
3
|
+
"createText": "Preencha as informações do usuário.",
|
|
4
|
+
"createTooltip": "Criar novo usuário",
|
|
5
|
+
"delete": "Excluir usuário",
|
|
6
|
+
"deleteText": "Você tem certeza que deseja excluir esses usuários?",
|
|
7
|
+
"deleteTooltip": "Excluir o usuário selecionado",
|
|
8
|
+
"edit": "Editar usuário",
|
|
9
|
+
"editText": "Visualizar e editar informações do usuário.",
|
|
10
|
+
"editTooltip": "Editar o usuário selecionado"
|
|
11
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useDefaultMutation } from "@/hooks/use-default-mutation";
|
|
2
|
+
import { useQuery } from "@tanstack/react-query";
|
|
3
|
+
import { requests } from "./requests";
|
|
4
|
+
|
|
5
|
+
const scope = "user";
|
|
6
|
+
|
|
7
|
+
export function useUserCreate() {
|
|
8
|
+
const { userCreate } = requests();
|
|
9
|
+
return useDefaultMutation(scope, "create", userCreate);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function useUserDelete() {
|
|
13
|
+
const { userDelete } = requests();
|
|
14
|
+
return useDefaultMutation(scope, "delete", userDelete);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function useUserUpdate() {
|
|
18
|
+
const { userUpdate } = requests();
|
|
19
|
+
return useDefaultMutation(scope, "update", userUpdate);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function useUserGet(id: number) {
|
|
23
|
+
const { userGet } = requests();
|
|
24
|
+
return useQuery({
|
|
25
|
+
queryKey: [scope, "get"],
|
|
26
|
+
queryFn: () => userGet(id),
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useApp } from "@/hooks/use-app";
|
|
2
|
+
import { Delete, PaginationParams, PaginationResult } from "@/types";
|
|
3
|
+
import { User } from "@/types/models";
|
|
4
|
+
import { HttpMethod } from "@/types/http-method";
|
|
5
|
+
|
|
6
|
+
export function requests() {
|
|
7
|
+
const { request } = useApp();
|
|
8
|
+
|
|
9
|
+
const userList = async (params: PaginationParams) => {
|
|
10
|
+
return request<PaginationResult<User>>({
|
|
11
|
+
url: "/user",
|
|
12
|
+
params,
|
|
13
|
+
}).then((res) => res.data);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const userGet = async (id: number) => {
|
|
17
|
+
return request<User>({
|
|
18
|
+
url: `/user/${id}`,
|
|
19
|
+
}).then((res) => res.data);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const userCreate = async (params: { data: User }) => {
|
|
23
|
+
const { data } = params;
|
|
24
|
+
return request<User>({
|
|
25
|
+
url: "/user",
|
|
26
|
+
method: HttpMethod.POST,
|
|
27
|
+
data: data,
|
|
28
|
+
}).then((res) => res.data);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const userDelete = async (ids: number[]) => {
|
|
32
|
+
return request<Delete>({
|
|
33
|
+
url: "/user",
|
|
34
|
+
data: { ids },
|
|
35
|
+
method: HttpMethod.DELETE,
|
|
36
|
+
}).then((res) => res.data);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const userUpdate = async (params: { id: number; data: User }) => {
|
|
40
|
+
const { id, data } = params;
|
|
41
|
+
return request<User>({
|
|
42
|
+
url: `/user/${id}`,
|
|
43
|
+
method: HttpMethod.PATCH,
|
|
44
|
+
data: data,
|
|
45
|
+
}).then((res) => res.data);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
userCreate,
|
|
50
|
+
userUpdate,
|
|
51
|
+
userDelete,
|
|
52
|
+
userList,
|
|
53
|
+
userGet,
|
|
54
|
+
};
|
|
55
|
+
}
|
package/package.json
CHANGED
|
@@ -1,43 +1,43 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@hedhog/admin",
|
|
3
|
-
"version": "0.12.
|
|
4
|
-
"private": false,
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"clean": "rimraf ./dist",
|
|
8
|
-
"prebuild": "npm run clean",
|
|
9
|
-
"build": "tsc --project tsconfig.production.json && npm version patch",
|
|
10
|
-
"prod": "npm run build && npm publish --access public"
|
|
11
|
-
},
|
|
12
|
-
"file": [
|
|
13
|
-
"dist/**/*",
|
|
14
|
-
"src/entities/**/*.ts",
|
|
15
|
-
"src/migrations/**/*.ts",
|
|
16
|
-
"src/**/*.ejs",
|
|
17
|
-
"hedhog.yaml"
|
|
18
|
-
],
|
|
19
|
-
"keywords": [],
|
|
20
|
-
"author": "",
|
|
21
|
-
"license": "MIT",
|
|
22
|
-
"description": "",
|
|
23
|
-
"devDependencies": {
|
|
24
|
-
"@hedhog/locale": "^0.
|
|
25
|
-
"@hedhog/mail": "^0.
|
|
26
|
-
"@hedhog/pagination": "^0.
|
|
27
|
-
"@hedhog/prisma": "^0.
|
|
28
|
-
"@hedhog/setting": "^0.
|
|
29
|
-
"@hedhog/utils": "^0.0.28",
|
|
30
|
-
"@nestjs/mapped-types": "^2.0.5",
|
|
31
|
-
"ts-node": "^10.9.1",
|
|
32
|
-
"typescript": "^5.1.3"
|
|
33
|
-
},
|
|
34
|
-
"peerDependencies": {
|
|
35
|
-
"@hedhog/locale": "latest",
|
|
36
|
-
"@hedhog/mail": "latest",
|
|
37
|
-
"@hedhog/pagination": "latest",
|
|
38
|
-
"@hedhog/prisma": "latest",
|
|
39
|
-
"@hedhog/setting": "latest",
|
|
40
|
-
"@hedhog/utils": "latest",
|
|
41
|
-
"@nestjs/jwt": "latest"
|
|
42
|
-
}
|
|
43
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@hedhog/admin",
|
|
3
|
+
"version": "0.12.3",
|
|
4
|
+
"private": false,
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"clean": "rimraf ./dist",
|
|
8
|
+
"prebuild": "npm run clean",
|
|
9
|
+
"build": "tsc --project tsconfig.production.json && npm version patch",
|
|
10
|
+
"prod": "npm run build && npm publish --access public"
|
|
11
|
+
},
|
|
12
|
+
"file": [
|
|
13
|
+
"dist/**/*",
|
|
14
|
+
"src/entities/**/*.ts",
|
|
15
|
+
"src/migrations/**/*.ts",
|
|
16
|
+
"src/**/*.ejs",
|
|
17
|
+
"hedhog.yaml"
|
|
18
|
+
],
|
|
19
|
+
"keywords": [],
|
|
20
|
+
"author": "",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"description": "",
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@hedhog/locale": "^0.17.1",
|
|
25
|
+
"@hedhog/mail": "^0.17.1",
|
|
26
|
+
"@hedhog/pagination": "^0.17.1",
|
|
27
|
+
"@hedhog/prisma": "^0.17.1",
|
|
28
|
+
"@hedhog/setting": "^0.12.2",
|
|
29
|
+
"@hedhog/utils": "^0.0.28",
|
|
30
|
+
"@nestjs/mapped-types": "^2.0.5",
|
|
31
|
+
"ts-node": "^10.9.1",
|
|
32
|
+
"typescript": "^5.1.3"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@hedhog/locale": "latest",
|
|
36
|
+
"@hedhog/mail": "latest",
|
|
37
|
+
"@hedhog/pagination": "latest",
|
|
38
|
+
"@hedhog/prisma": "latest",
|
|
39
|
+
"@hedhog/setting": "latest",
|
|
40
|
+
"@hedhog/utils": "latest",
|
|
41
|
+
"@nestjs/jwt": "latest"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -1,196 +1,196 @@
|
|
|
1
|
-
import { MailService } from '@hedhog/mail';
|
|
2
|
-
import { PrismaService } from '@hedhog/prisma';
|
|
3
|
-
import { NotFoundException } from '@nestjs/common';
|
|
4
|
-
import { JwtService } from '@nestjs/jwt';
|
|
5
|
-
import { Test, TestingModule } from '@nestjs/testing';
|
|
6
|
-
import { compare } from 'bcrypt';
|
|
7
|
-
import { AuthService } from './auth.service';
|
|
8
|
-
|
|
9
|
-
jest.mock('bcrypt');
|
|
10
|
-
|
|
11
|
-
describe('AuthService', () => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
});
|
|
1
|
+
// import { MailService } from '@hedhog/mail';
|
|
2
|
+
// import { PrismaService } from '@hedhog/prisma';
|
|
3
|
+
// import { NotFoundException } from '@nestjs/common';
|
|
4
|
+
// import { JwtService } from '@nestjs/jwt';
|
|
5
|
+
// import { Test, TestingModule } from '@nestjs/testing';
|
|
6
|
+
// import { compare } from 'bcrypt';
|
|
7
|
+
// import { AuthService } from './auth.service';
|
|
8
|
+
|
|
9
|
+
// jest.mock('bcrypt');
|
|
10
|
+
|
|
11
|
+
// describe('AuthService', () => {
|
|
12
|
+
// let authService: AuthService;
|
|
13
|
+
// let prismaService: PrismaService;
|
|
14
|
+
// let jwtService: JwtService;
|
|
15
|
+
// let mailService: MailService;
|
|
16
|
+
|
|
17
|
+
// beforeEach(async () => {
|
|
18
|
+
// const module: TestingModule = await Test.createTestingModule({
|
|
19
|
+
// providers: [
|
|
20
|
+
// AuthService,
|
|
21
|
+
// {
|
|
22
|
+
// provide: PrismaService,
|
|
23
|
+
// useValue: {
|
|
24
|
+
// user: {
|
|
25
|
+
// findFirst: jest.fn(),
|
|
26
|
+
// update: jest.fn(),
|
|
27
|
+
// findUnique: jest.fn(),
|
|
28
|
+
// },
|
|
29
|
+
// },
|
|
30
|
+
// },
|
|
31
|
+
// {
|
|
32
|
+
// provide: JwtService,
|
|
33
|
+
// useValue: {
|
|
34
|
+
// sign: jest.fn(),
|
|
35
|
+
// verifyAsync: jest.fn(),
|
|
36
|
+
// decode: jest.fn(),
|
|
37
|
+
// },
|
|
38
|
+
// },
|
|
39
|
+
// {
|
|
40
|
+
// provide: MailService,
|
|
41
|
+
// useValue: {
|
|
42
|
+
// send: jest.fn(),
|
|
43
|
+
// },
|
|
44
|
+
// },
|
|
45
|
+
// ],
|
|
46
|
+
// }).compile();
|
|
47
|
+
|
|
48
|
+
// authService = module.get<AuthService>(AuthService);
|
|
49
|
+
// prismaService = module.get<PrismaService>(PrismaService);
|
|
50
|
+
// jwtService = module.get<JwtService>(JwtService);
|
|
51
|
+
// mailService = module.get<MailService>(MailService);
|
|
52
|
+
// });
|
|
53
|
+
|
|
54
|
+
// it('should be defined', () => {
|
|
55
|
+
// expect(authService).toBeDefined();
|
|
56
|
+
// });
|
|
57
|
+
|
|
58
|
+
// describe('loginWithEmailAndPassword', () => {
|
|
59
|
+
// it('should throw NotFoundException if user is not found', async () => {
|
|
60
|
+
// jest.spyOn(prismaService.user, 'findFirst').mockResolvedValue(null);
|
|
61
|
+
|
|
62
|
+
// await expect(
|
|
63
|
+
// authService.loginWithEmailAndPassword('test@example.com', 'password'),
|
|
64
|
+
// ).rejects.toThrow(NotFoundException);
|
|
65
|
+
// });
|
|
66
|
+
|
|
67
|
+
// it('should throw NotFoundException if password is invalid', async () => {
|
|
68
|
+
// const mockUser = {
|
|
69
|
+
// id: 1,
|
|
70
|
+
// name: 'Test User',
|
|
71
|
+
// email: 'test@example.com',
|
|
72
|
+
// password: 'hashedpassword',
|
|
73
|
+
// multifactor_id: null,
|
|
74
|
+
// code: null,
|
|
75
|
+
// created_at: new Date(),
|
|
76
|
+
// updated_at: new Date(),
|
|
77
|
+
// };
|
|
78
|
+
// jest.spyOn(prismaService.user, 'findFirst').mockResolvedValue(mockUser);
|
|
79
|
+
// (compare as jest.Mock).mockResolvedValue(false);
|
|
80
|
+
|
|
81
|
+
// await expect(
|
|
82
|
+
// authService.loginWithEmailAndPassword('test@example.com', 'password'),
|
|
83
|
+
// ).rejects.toThrow(NotFoundException);
|
|
84
|
+
// });
|
|
85
|
+
|
|
86
|
+
// it('should return a token if login is successful', async () => {
|
|
87
|
+
// const mockUser = {
|
|
88
|
+
// id: 1,
|
|
89
|
+
// name: 'Test User',
|
|
90
|
+
// email: 'test@example.com',
|
|
91
|
+
// password: 'hashedpassword',
|
|
92
|
+
// multifactor_id: null,
|
|
93
|
+
// code: null,
|
|
94
|
+
// created_at: new Date(),
|
|
95
|
+
// updated_at: new Date(),
|
|
96
|
+
// };
|
|
97
|
+
// jest.spyOn(prismaService.user, 'findFirst').mockResolvedValue(mockUser);
|
|
98
|
+
// (compare as jest.Mock).mockResolvedValue(true);
|
|
99
|
+
// jest.spyOn(jwtService, 'sign').mockReturnValue('token');
|
|
100
|
+
|
|
101
|
+
// const result = await authService.loginWithEmailAndPassword(
|
|
102
|
+
// 'test@example.com',
|
|
103
|
+
// 'password',
|
|
104
|
+
// );
|
|
105
|
+
|
|
106
|
+
// expect(result).toEqual({ token: 'token' });
|
|
107
|
+
// });
|
|
108
|
+
// });
|
|
109
|
+
|
|
110
|
+
// describe('forget', () => {
|
|
111
|
+
// it('should throw NotFoundException if user is not found', async () => {
|
|
112
|
+
// jest.spyOn(prismaService.user, 'findFirst').mockResolvedValue(null);
|
|
113
|
+
|
|
114
|
+
// await expect(
|
|
115
|
+
// authService.forget({ email: 'test@example.com' }),
|
|
116
|
+
// ).rejects.toThrow(NotFoundException);
|
|
117
|
+
// });
|
|
118
|
+
|
|
119
|
+
// it('should update user with reset code and send email', async () => {
|
|
120
|
+
// const mockUser = {
|
|
121
|
+
// id: 1,
|
|
122
|
+
// name: 'Test User',
|
|
123
|
+
// email: 'test@example.com',
|
|
124
|
+
// password: 'hashedpassword',
|
|
125
|
+
// multifactor_id: null,
|
|
126
|
+
// code: null,
|
|
127
|
+
// created_at: new Date(),
|
|
128
|
+
// updated_at: new Date(),
|
|
129
|
+
// };
|
|
130
|
+
// jest.spyOn(prismaService.user, 'findFirst').mockResolvedValue(mockUser);
|
|
131
|
+
// jest.spyOn(prismaService.user, 'update').mockResolvedValue(mockUser);
|
|
132
|
+
// jest
|
|
133
|
+
// .spyOn(authService, 'generateRandomString')
|
|
134
|
+
// .mockReturnValue('resetCode');
|
|
135
|
+
|
|
136
|
+
// const result = await authService.forget({ email: 'test@example.com' });
|
|
137
|
+
|
|
138
|
+
// expect(prismaService.user.update).toHaveBeenCalledWith({
|
|
139
|
+
// where: { id: 1 },
|
|
140
|
+
// data: { code: 'resetCode' },
|
|
141
|
+
// });
|
|
142
|
+
// expect(mailService.send).toHaveBeenCalledWith({
|
|
143
|
+
// to: 'test@example.com',
|
|
144
|
+
// subject: 'Reset password',
|
|
145
|
+
// body: `Reset your password by clicking <a href="${process.env.FRONTEND_URL}/reset-password/resetCode">here</a>`,
|
|
146
|
+
// });
|
|
147
|
+
// expect(result).toEqual(true);
|
|
148
|
+
// });
|
|
149
|
+
// });
|
|
150
|
+
|
|
151
|
+
// describe('otp', () => {
|
|
152
|
+
// it('should throw NotFoundException if user with code is not found', async () => {
|
|
153
|
+
// jest.spyOn(jwtService, 'decode').mockReturnValue({ id: 1 });
|
|
154
|
+
// jest.spyOn(prismaService.user, 'findFirst').mockResolvedValue(null);
|
|
155
|
+
|
|
156
|
+
// await expect(
|
|
157
|
+
// authService.otp({ token: 'token', code: 123456 }),
|
|
158
|
+
// ).rejects.toThrow(NotFoundException);
|
|
159
|
+
// });
|
|
160
|
+
|
|
161
|
+
// it('should return a token if otp is valid', async () => {
|
|
162
|
+
// const mockUser = {
|
|
163
|
+
// id: 1,
|
|
164
|
+
// name: 'Test User',
|
|
165
|
+
// email: 'test@example.com',
|
|
166
|
+
// password: 'hashedpassword',
|
|
167
|
+
// multifactor_id: null,
|
|
168
|
+
// code: null,
|
|
169
|
+
// created_at: new Date(),
|
|
170
|
+
// updated_at: new Date(),
|
|
171
|
+
// };
|
|
172
|
+
// jest.spyOn(jwtService, 'decode').mockReturnValue({ id: 1 });
|
|
173
|
+
// jest.spyOn(prismaService.user, 'findFirst').mockResolvedValue(mockUser);
|
|
174
|
+
// jest.spyOn(prismaService.user, 'update').mockResolvedValue(mockUser);
|
|
175
|
+
// jest.spyOn(jwtService, 'sign').mockReturnValue('newToken');
|
|
176
|
+
|
|
177
|
+
// const result = await authService.otp({ token: 'token', code: 123456 });
|
|
178
|
+
|
|
179
|
+
// expect(prismaService.user.update).toHaveBeenCalledWith({
|
|
180
|
+
// where: { id: 1 },
|
|
181
|
+
// data: { code: null },
|
|
182
|
+
// });
|
|
183
|
+
// expect(result).toEqual({ token: 'newToken' });
|
|
184
|
+
// });
|
|
185
|
+
// });
|
|
186
|
+
|
|
187
|
+
// describe('verifyToken', () => {
|
|
188
|
+
// it('should verify and return token data', async () => {
|
|
189
|
+
// jest.spyOn(jwtService, 'verifyAsync').mockResolvedValue({ id: 1 });
|
|
190
|
+
|
|
191
|
+
// const result = await authService.verifyToken('token');
|
|
192
|
+
|
|
193
|
+
// expect(result).toEqual({ id: 1 });
|
|
194
|
+
// });
|
|
195
|
+
// });
|
|
196
|
+
// });
|