Skip to content

Commit

Permalink
feat: account scope
Browse files Browse the repository at this point in the history
  • Loading branch information
guiseek committed Oct 1, 2023
1 parent 561c7bc commit 918ec1f
Show file tree
Hide file tree
Showing 250 changed files with 5,015 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ DB_USER=postgres
DB_PASS=postgres
DB_NAME=database

JWT_SECRET='DO NOT USE THIS VALUE. INSTEAD, CREATE A COMPLEX SECRET AND KEEP IT SAFE OUTSIDE OF THE SOURCE CODE.'

PORT=3000
25 changes: 25 additions & 0 deletions account/data-access/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
11 changes: 11 additions & 0 deletions account/data-access/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# account-data-access

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build account-data-access` to build the library.

## Running unit tests

Run `nx test account-data-access` to execute the unit tests via [Jest](https://jestjs.io).
10 changes: 10 additions & 0 deletions account/data-access/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable */
export default {
displayName: 'account-data-access',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['ts-jest', {tsconfig: '<rootDir>/tsconfig.spec.json'}],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/account/data-access',
}
10 changes: 10 additions & 0 deletions account/data-access/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@dev/account-data-access",
"version": "0.0.1",
"dependencies": {
"tslib": "^2.3.0"
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts"
}
43 changes: 43 additions & 0 deletions account/data-access/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "account-data-access",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "account/data-access/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/account/data-access",
"main": "account/data-access/src/index.ts",
"tsConfig": "account/data-access/tsconfig.lib.json",
"assets": ["account/data-access/*.md"]
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"account/data-access/**/*.ts",
"account/data-access/package.json"
]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "account/data-access/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
}
},
"tags": ["type:data-access", "scope:account", "side:client"]
}
1 change: 1 addition & 0 deletions account/data-access/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './public-api'
53 changes: 53 additions & 0 deletions account/data-access/src/lib/application/auth.facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {AuthService, AuthUser, CreateUser, SignIn} from '@dev/account-domain'
import {Store} from '@dev/shared-data-access'
import {switchMap, take, tap} from 'rxjs'

interface AuthState {
authUser: AuthUser | null
warning: string | null
loading: boolean
}
export class AuthFacade extends Store<AuthState> {
authUser$ = this.select((state) => state.authUser)
warning$ = this.select((state) => state.warning)
loading$ = this.select((state) => state.loading)

get accessToken() {
return this.authService.accessToken
}

constructor(private authService: AuthService) {
super({authUser: null, warning: null, loading: false})
}

login<T extends SignIn>(signIn: T) {
this.setState({loading: true})
const authUser$ = this.authService.login(signIn).pipe(
switchMap(() => this.authService.me()),
take(1)
)

authUser$.subscribe((authUser) => this.setState({authUser, loading: false}))
}

me() {
return this.authService.me().pipe(
tap((authUser) => this.setState({authUser})),
take(1)
)
}

register(createUser: CreateUser) {
this.setState({loading: true})
const createUser$ = this.authService.register(createUser).pipe(take(1))

createUser$.subscribe((authUser) =>
this.setState({authUser, loading: false})
)
}

logout() {
this.authService.accessToken = null
this.setState({authUser: null})
}
}
4 changes: 4 additions & 0 deletions account/data-access/src/lib/application/group.facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {Facade} from '@dev/shared-util-data'
import {Group} from '@dev/account-domain'

export class GroupFacade extends Facade<Group> {}
3 changes: 3 additions & 0 deletions account/data-access/src/lib/application/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './auth.facade'
export * from './group.facade'
export * from './user.facade'
17 changes: 17 additions & 0 deletions account/data-access/src/lib/application/user.facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {UpdatePassword, User, UserService} from '@dev/account-domain'
import {Facade} from '@dev/shared-util-data'
import {catchError} from 'rxjs'

export class UserFacade extends Facade<User, UserService> {
constructor(service: UserService) {
super(service)
}

updatePassword(value: UpdatePassword) {
const update$ = this.service.updatePassword(value)
update$.pipe(catchError(this.handleError)).subscribe((selected) => {
this.setState({selected})
this.findOne(value.id)
})
}
}
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/create-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {CreateGroup} from '@dev/account-domain'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/create-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {CreateUser} from '@dev/account-domain'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {Group} from '@dev/account-domain'
8 changes: 8 additions & 0 deletions account/data-access/src/lib/dtos/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from './create-group'
export * from './create-user'
export * from './group'
export * from './sign-in'
export * from './update-group'
export * from './update-password'
export * from './update-user'
export * from './user'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/sign-in.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {SignIn} from '@dev/account-domain'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/update-group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {UpdateGroup} from '@dev/account-domain'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/update-password.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {UpdatePassword} from '@dev/account-domain'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/update-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {UpdateUser} from '@dev/account-domain'
1 change: 1 addition & 0 deletions account/data-access/src/lib/dtos/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {User} from '@dev/account-domain'
36 changes: 36 additions & 0 deletions account/data-access/src/lib/infrastructure/auth.service.impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
SignIn,
AuthUser,
AccessToken,
AuthService,
CreateUser,
} from '@dev/account-domain'
import {Http} from '@dev/shared-data-access'
import {take, tap} from 'rxjs'

export class AuthServiceImpl implements AuthService {
get accessToken() {
return localStorage.getItem('accessToken')
}
set accessToken(value) {
if (value) localStorage.setItem('accessToken', value)
else localStorage.removeItem('accessToken')
}

constructor(private readonly http: Http, readonly url: string) {}

login(value: SignIn) {
return this.http.post<AccessToken>(`${this.url}/login`, value).pipe(
tap(({accessToken}) => (this.accessToken = accessToken)),
take(1)
)
}

register(value: CreateUser) {
return this.http.post<AuthUser>(`${this.url}/register`, value).pipe(take(1))
}

me() {
return this.http.get<AuthUser>(`${this.url}/me`).pipe(take(1))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {Group, GroupService} from '@dev/account-domain'
import {BaseService} from '@dev/shared-data-access'

export class GroupServiceImpl
extends BaseService<Group>
implements GroupService {}
19 changes: 19 additions & 0 deletions account/data-access/src/lib/infrastructure/group.service.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Group, GroupService} from '@dev/account-domain'
import {MockService} from '@dev/shared-data-access'
import {of} from 'rxjs'

export class GroupServiceMock
extends MockService<Group>
implements GroupService
{
create(value: Partial<Group>) {
const entity = {
...value,
id: crypto.randomUUID(),
createdAt: new Date(),
updatedAt: new Date(),
} as Group
this.collection.push(entity)
return of(entity)
}
}
5 changes: 5 additions & 0 deletions account/data-access/src/lib/infrastructure/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './auth.service.impl'
export * from './group.service.impl'
export * from './group.service.mock'
export * from './user.service.impl'
export * from './user.service.mock'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {UpdatePassword, User, UserService} from '@dev/account-domain'
import {BaseService} from '@dev/shared-data-access'
import {Observable} from 'rxjs'

export class UserServiceImpl extends BaseService<User> implements UserService {
updatePassword(value: UpdatePassword): Observable<User> {
return this.http.put(`${this.url}/password`, value)
}
}
20 changes: 20 additions & 0 deletions account/data-access/src/lib/infrastructure/user.service.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {UpdatePassword, User, UserService} from '@dev/account-domain'
import {MockService} from '@dev/shared-data-access'
import {of} from 'rxjs'

export class UserServiceMock extends MockService<User> implements UserService {
updatePassword(value: UpdatePassword) {
return this.findOne(value.id)
}

create(value: Partial<User>) {
const entity = {
...value,
id: crypto.randomUUID(),
createdAt: new Date(),
updatedAt: new Date(),
} as User
this.collection.push(entity)
return of(entity)
}
}
29 changes: 29 additions & 0 deletions account/data-access/src/lib/providers/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {AuthService} from '@dev/account-domain'
import {AuthServiceImpl} from '../infrastructure'
import {Http} from '@dev/shared-data-access'
import {AuthFacade} from '../application'

export function getAuthService() {
return AuthService
}

export function provideAuthService(url: string) {
return {
provide: getAuthService(),
useFactory(http: Http) {
return new AuthServiceImpl(http, url)
},
deps: [Http],
}
}

export function provideAuthFacade() {
return {
provide: AuthFacade,
deps: [getAuthService()],
}
}

export function provideAuth(url: string) {
return [provideAuthService(url), provideAuthFacade()]
}
37 changes: 37 additions & 0 deletions account/data-access/src/lib/providers/group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {GroupServiceImpl, GroupServiceMock} from '../infrastructure'
import {Http, provideServiceMock} from '@dev/shared-data-access'
import {Group, GroupService} from '@dev/account-domain'
import {GroupFacade} from '../application'

export function provideGroupService(api: string) {
return {
provide: GroupService,
useFactory(http: Http) {
return new GroupServiceImpl(http, api)
},
deps: [Http],
}
}

export function provideGroupServiceMock(collection: Group[] = []) {
return provideServiceMock(GroupService, GroupServiceMock, collection)
}

export function provideGroupFacade() {
return {
provide: GroupFacade,
deps: [GroupService],
}
}

export function provideGroup(production = false, api: string | Group[] = []) {
const providers = []
providers.push(
production
? provideGroupService(api as string)
: provideGroupServiceMock(api as Group[])
)

providers.push(provideGroupFacade())
return providers
}
3 changes: 3 additions & 0 deletions account/data-access/src/lib/providers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './auth'
export * from './group'
export * from './user'
Loading

0 comments on commit 918ec1f

Please sign in to comment.