From b13ffeea4e4c1a6e0b4e98680e2c3206ca7e3b0c Mon Sep 17 00:00:00 2001 From: JUNG YI DAM Date: Wed, 13 Jul 2022 05:58:28 +0000 Subject: [PATCH] =?UTF-8?q?=EC=BF=A0=ED=8F=B0=EB=B0=9C=ED=96=89=EB=A6=AC?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20page=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/app.routing.ts | 7 + src/app/mock-api/apps/member/coupon/api.ts | 217 +++++++++++ src/app/mock-api/apps/member/coupon/data.ts | 33 ++ src/app/mock-api/common/navigation/data.ts | 7 + src/app/mock-api/index.ts | 2 + .../admin/member/coupon/components/index.ts | 3 + .../coupon/components/list.component.html | 355 ++++++++++++++++++ .../coupon/components/list.component.ts | 198 ++++++++++ .../admin/member/coupon/coupon.module.ts | 50 +++ .../admin/member/coupon/coupon.routing.ts | 24 ++ .../member/coupon/models/coupon-pagination.ts | 8 + .../admin/member/coupon/models/coupon.ts | 29 ++ .../coupon/resolvers/coupon.resolver.ts | 84 +++++ .../member/coupon/services/coupon.service.ts | 153 ++++++++ src/assets/i18n/en.json | 1 + src/assets/i18n/ko.json | 1 + 16 files changed, 1172 insertions(+) create mode 100644 src/app/mock-api/apps/member/coupon/api.ts create mode 100644 src/app/mock-api/apps/member/coupon/data.ts create mode 100644 src/app/modules/admin/member/coupon/components/index.ts create mode 100644 src/app/modules/admin/member/coupon/components/list.component.html create mode 100644 src/app/modules/admin/member/coupon/components/list.component.ts create mode 100644 src/app/modules/admin/member/coupon/coupon.module.ts create mode 100644 src/app/modules/admin/member/coupon/coupon.routing.ts create mode 100644 src/app/modules/admin/member/coupon/models/coupon-pagination.ts create mode 100644 src/app/modules/admin/member/coupon/models/coupon.ts create mode 100644 src/app/modules/admin/member/coupon/resolvers/coupon.resolver.ts create mode 100644 src/app/modules/admin/member/coupon/services/coupon.service.ts diff --git a/src/app/app.routing.ts b/src/app/app.routing.ts index 5c3a3dc..cc75f7b 100644 --- a/src/app/app.routing.ts +++ b/src/app/app.routing.ts @@ -220,6 +220,13 @@ export const appRoutes: Route[] = [ 'app/modules/admin/member/partner-recommendation/partner-recommendation.module' ).then((m: any) => m.PartnerRecommendationModule), }, + { + path: 'coupon', + loadChildren: () => + import('app/modules/admin/member/coupon/coupon.module').then( + (m: any) => m.CouponModule + ), + }, ], }, { diff --git a/src/app/mock-api/apps/member/coupon/api.ts b/src/app/mock-api/apps/member/coupon/api.ts new file mode 100644 index 0000000..2d704fb --- /dev/null +++ b/src/app/mock-api/apps/member/coupon/api.ts @@ -0,0 +1,217 @@ +import { Injectable } from '@angular/core'; +import { assign, cloneDeep } from 'lodash-es'; +import { FuseMockApiService, FuseMockApiUtils } from '@fuse/lib/mock-api'; +import { coupons as couponsData } from './data'; + +@Injectable({ + providedIn: 'root', +}) +export class MemberCouponMockApi { + private _coupons: any[] = couponsData; + + /** + * Constructor + */ + constructor(private _fuseMockApiService: FuseMockApiService) { + // Register Mock API handlers + this.registerHandlers(); + } + + // ----------------------------------------------------------------------------------------------------- + // @ Public methods + // ----------------------------------------------------------------------------------------------------- + + /** + * Register Mock API handlers + */ + registerHandlers(): void { + // ----------------------------------------------------------------------------------------------------- + // @ Coupons - GET + // ----------------------------------------------------------------------------------------------------- + this._fuseMockApiService + .onGet('api/apps/member/coupon/coupons', 300) + .reply(({ request }) => { + // Get available queries + const search = request.params.get('search'); + const sort = request.params.get('sort') || 'name'; + const order = request.params.get('order') || 'asc'; + const page = parseInt(request.params.get('page') ?? '1', 10); + const size = parseInt(request.params.get('size') ?? '10', 10); + + // Clone the coupons + let coupons: any[] | null = cloneDeep(this._coupons); + + // Sort the coupons + if (sort === 'sku' || sort === 'name' || sort === 'active') { + coupons.sort((a, b) => { + const fieldA = a[sort].toString().toUpperCase(); + const fieldB = b[sort].toString().toUpperCase(); + return order === 'asc' + ? fieldA.localeCompare(fieldB) + : fieldB.localeCompare(fieldA); + }); + } else { + coupons.sort((a, b) => + order === 'asc' ? a[sort] - b[sort] : b[sort] - a[sort] + ); + } + + // If search exists... + if (search) { + // Filter the coupons + coupons = coupons.filter( + (contact: any) => + contact.name && + contact.name.toLowerCase().includes(search.toLowerCase()) + ); + } + + // Paginate - Start + const couponsLength = coupons.length; + + // Calculate pagination details + const begin = page * size; + const end = Math.min(size * (page + 1), couponsLength); + const lastPage = Math.max(Math.ceil(couponsLength / size), 1); + + // Prepare the pagination object + let pagination = {}; + + // If the requested page number is bigger than + // the last possible page number, return null for + // coupons but also send the last possible page so + // the app can navigate to there + if (page > lastPage) { + coupons = null; + pagination = { + lastPage, + }; + } else { + // Paginate the results by size + coupons = coupons.slice(begin, end); + + // Prepare the pagination mock-api + pagination = { + length: couponsLength, + size: size, + page: page, + lastPage: lastPage, + startIndex: begin, + endIndex: end - 1, + }; + } + + // Return the response + return [ + 200, + { + coupons, + pagination, + }, + ]; + }); + + // ----------------------------------------------------------------------------------------------------- + // @ Coupon - GET + // ----------------------------------------------------------------------------------------------------- + this._fuseMockApiService + .onGet('api/apps/member/coupon/coupon') + .reply(({ request }) => { + // Get the id from the params + const id = request.params.get('id'); + + // Clone the coupons + const coupons = cloneDeep(this._coupons); + + // Find the coupon + const coupon = coupons.find((item: any) => item.id === id); + + // Return the response + return [200, coupon]; + }); + + // ----------------------------------------------------------------------------------------------------- + // @ Coupon - POST + // ----------------------------------------------------------------------------------------------------- + this._fuseMockApiService + .onPost('api/apps/member/coupon/coupon') + .reply(() => { + // Generate a new coupon + const newCoupon = { + id: FuseMockApiUtils.guid(), + category: '', + name: 'A New User', + description: '', + tags: [], + sku: '', + barcode: '', + brand: '', + vendor: '', + stock: '', + reserved: '', + cost: '', + basePrice: '', + taxPercent: '', + price: '', + weight: '', + thumbnail: '', + images: [], + active: false, + }; + + // Unshift the new coupon + this._coupons.unshift(newCoupon); + + // Return the response + return [200, newCoupon]; + }); + + // ----------------------------------------------------------------------------------------------------- + // @ Coupon - PATCH + // ----------------------------------------------------------------------------------------------------- + this._fuseMockApiService + .onPatch('api/apps/member/coupon/coupon') + .reply(({ request }) => { + // Get the id and coupon + const id = request.body.id; + const coupon = cloneDeep(request.body.coupon); + + // Prepare the updated coupon + let updatedCoupon = null; + + // Find the coupon and update it + this._coupons.forEach((item, index, coupons) => { + if (item.id === id) { + // Update the coupon + coupons[index] = assign({}, coupons[index], coupon); + + // Store the updated coupon + updatedCoupon = coupons[index]; + } + }); + + // Return the response + return [200, updatedCoupon]; + }); + + // ----------------------------------------------------------------------------------------------------- + // @ Coupon - DELETE + // ----------------------------------------------------------------------------------------------------- + this._fuseMockApiService + .onDelete('api/apps/member/coupon/coupon') + .reply(({ request }) => { + // Get the id + const id = request.params.get('id'); + + // Find the coupon and delete it + this._coupons.forEach((item, index) => { + if (item.id === id) { + this._coupons.splice(index, 1); + } + }); + + // Return the response + return [200, true]; + }); + } +} diff --git a/src/app/mock-api/apps/member/coupon/data.ts b/src/app/mock-api/apps/member/coupon/data.ts new file mode 100644 index 0000000..6b24003 --- /dev/null +++ b/src/app/mock-api/apps/member/coupon/data.ts @@ -0,0 +1,33 @@ +/* eslint-disable */ + +export const coupons = [ + { + id: 'on00', + totalPartnerCount: '5', + totalHoldingMoney: 303675, + totalComp: 108933, + total: 412608, + branchCount: 1, + divisionCount: 1, + officeCount: 1, + storeCount: 1, + memberCount: 1, + nickname: 'on00', + accountHolder: '11', + phoneNumber: '010-1111-1111', + calculateType: '롤링', + ownCash: 50000, + ownComp: 1711, + ownCoupon: 50000, + gameMoney: 0, + todayComp: 0, + totalDeposit: 0, + totalWithdraw: 0, + balance: 0, + registDate: '2022-06-12 15:38', + finalSigninDate: '', + ip: '', + state: '정상', + note: '', + }, +]; diff --git a/src/app/mock-api/common/navigation/data.ts b/src/app/mock-api/common/navigation/data.ts index 69d881f..570b663 100644 --- a/src/app/mock-api/common/navigation/data.ts +++ b/src/app/mock-api/common/navigation/data.ts @@ -116,6 +116,13 @@ export const defaultNavigation: FuseNavigationItem[] = [ icon: 'heroicons_outline:academic-cap', link: '/member/partner-recommendation', }, + { + id: 'member.coupon', + title: 'Coupon', + type: 'basic', + icon: 'heroicons_outline:academic-cap', + link: '/member/coupon', + }, ], }, { diff --git a/src/app/mock-api/index.ts b/src/app/mock-api/index.ts index da9f986..8f2a377 100644 --- a/src/app/mock-api/index.ts +++ b/src/app/mock-api/index.ts @@ -21,6 +21,7 @@ import { MemberPartnerDivisionMockApi } from './apps/member/partner-division/api import { MemberPartnerOfficeMockApi } from './apps/member/partner-office/api'; import { MemberPartnerStoreMockApi } from './apps/member/partner-store/api'; import { MemberPartnerRecommendationMockApi } from './apps/member/partner-recommendation/api'; +import { MemberCouponMockApi } from './apps/member/coupon/api'; import { MessagesMockApi } from 'app/mock-api/common/messages/api'; import { NavigationMockApi } from 'app/mock-api/common/navigation/api'; import { NotesMockApi } from 'app/mock-api/apps/notes/api'; @@ -62,6 +63,7 @@ export const mockApiServices = [ MemberPartnerOfficeMockApi, MemberPartnerStoreMockApi, MemberPartnerRecommendationMockApi, + MemberCouponMockApi, MessagesMockApi, NavigationMockApi, NotesMockApi, diff --git a/src/app/modules/admin/member/coupon/components/index.ts b/src/app/modules/admin/member/coupon/components/index.ts new file mode 100644 index 0000000..04759eb --- /dev/null +++ b/src/app/modules/admin/member/coupon/components/index.ts @@ -0,0 +1,3 @@ +import { ListComponent } from './list.component'; + +export const COMPONENTS = [ListComponent]; diff --git a/src/app/modules/admin/member/coupon/components/list.component.html b/src/app/modules/admin/member/coupon/components/list.component.html new file mode 100644 index 0000000..0da6106 --- /dev/null +++ b/src/app/modules/admin/member/coupon/components/list.component.html @@ -0,0 +1,355 @@ +
+ +
+ +
+ +
+ +
쿠폰발행리스트
+ +
+ + + + + + + 40 + 60 + 80 + 100 + + + + + LV.1 + LV.2 + LV.3 + LV.4 + + + + + 정상 + 대기 + 탈퇴 + 휴면 + 블랙 + 정지 + + + + + 카지노제한 + 슬롯제한 + + + + + 계좌입금 + + + + + 카지노콤프 + 슬롯콤프 + 배팅콤프 + 첫충콤프 + + + + + + + + + + + + +
+
+ + +
+ +
+ + +
+ +
+ + + + + + + + + + + + + + + + + + + + +
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+
+ + +
+
+ + +
+ There are no coupons! +
+
+
+
+
diff --git a/src/app/modules/admin/member/coupon/components/list.component.ts b/src/app/modules/admin/member/coupon/components/list.component.ts new file mode 100644 index 0000000..1bb948e --- /dev/null +++ b/src/app/modules/admin/member/coupon/components/list.component.ts @@ -0,0 +1,198 @@ +import { + AfterViewInit, + ChangeDetectionStrategy, + ChangeDetectorRef, + Component, + OnDestroy, + OnInit, + ViewChild, + ViewEncapsulation, +} from '@angular/core'; +import { + FormBuilder, + FormControl, + FormGroup, + Validators, +} from '@angular/forms'; +import { MatCheckboxChange } from '@angular/material/checkbox'; +import { MatPaginator } from '@angular/material/paginator'; +import { MatSort } from '@angular/material/sort'; +import { + debounceTime, + map, + merge, + Observable, + Subject, + switchMap, + takeUntil, +} from 'rxjs'; +import { fuseAnimations } from '@fuse/animations'; +import { FuseConfirmationService } from '@fuse/services/confirmation'; + +import { User } from '../../user/models/user'; +import { Coupon } from '../models/coupon'; +import { CouponPagination } from '../models/coupon-pagination'; +import { CouponService } from '../services/coupon.service'; +import { Router } from '@angular/router'; + +@Component({ + selector: 'partner-branch-list', + templateUrl: './list.component.html', + styles: [ + /* language=SCSS */ + ` + .inventory-grid { + grid-template-columns: 60px auto 40px; + + @screen sm { + grid-template-columns: 60px 60px 60px 60px 60px 60px auto 60px; + } + + @screen md { + grid-template-columns: 60px 60px 60px 60px 60px 60px auto 60px 60px; + } + + @screen lg { + grid-template-columns: 60px 70px 70px 70px 70px 100px 60px 60px auto 60px 60px 60px 60px; + } + } + `, + ], + encapsulation: ViewEncapsulation.None, + changeDetection: ChangeDetectionStrategy.OnPush, + animations: fuseAnimations, +}) +export class ListComponent implements OnInit, AfterViewInit, OnDestroy { + @ViewChild(MatPaginator) private _paginator!: MatPaginator; + @ViewChild(MatSort) private _sort!: MatSort; + + coupons$!: Observable; + users$!: Observable; + + isLoading = false; + searchInputControl = new FormControl(); + selectedCoupon?: Coupon; + pagination?: CouponPagination; + + private _unsubscribeAll: Subject = new Subject(); + + /** + * Constructor + */ + constructor( + private _changeDetectorRef: ChangeDetectorRef, + private _fuseConfirmationService: FuseConfirmationService, + private _formBuilder: FormBuilder, + private _couponService: CouponService, + private router: Router + ) {} + + // ----------------------------------------------------------------------------------------------------- + // @ Lifecycle hooks + // ----------------------------------------------------------------------------------------------------- + + /** + * On init + */ + ngOnInit(): void { + // Get the pagination + this._couponService.pagination$ + .pipe(takeUntil(this._unsubscribeAll)) + .subscribe((pagination: CouponPagination | undefined) => { + // Update the pagination + this.pagination = pagination; + + // Mark for check + this._changeDetectorRef.markForCheck(); + }); + + // Get the products + this.coupons$ = this._couponService.coupons$; + } + + /** + * After view init + */ + ngAfterViewInit(): void { + if (this._sort && this._paginator) { + // Set the initial sort + this._sort.sort({ + id: 'name', + start: 'asc', + disableClear: true, + }); + + // Mark for check + this._changeDetectorRef.markForCheck(); + + // If the coupon changes the sort order... + this._sort.sortChange + .pipe(takeUntil(this._unsubscribeAll)) + .subscribe(() => { + // Reset back to the first page + this._paginator.pageIndex = 0; + }); + + // Get products if sort or page changes + merge(this._sort.sortChange, this._paginator.page) + .pipe( + switchMap(() => { + this.isLoading = true; + return this._couponService.getCoupons( + this._paginator.pageIndex, + this._paginator.pageSize, + this._sort.active, + this._sort.direction + ); + }), + map(() => { + this.isLoading = false; + }) + ) + .subscribe(); + } + } + + /** + * On destroy + */ + ngOnDestroy(): void { + // Unsubscribe from all subscriptions + this._unsubscribeAll.next(null); + this._unsubscribeAll.complete(); + } + + // ----------------------------------------------------------------------------------------------------- + // @ Public methods + // ----------------------------------------------------------------------------------------------------- + + viewUserDetail(id: string): void { + let url: string = 'member/user/' + id; + this.router.navigateByUrl(url); + } + // ----------------------------------------------------------------------------------------------------- + // @ Private methods + // ----------------------------------------------------------------------------------------------------- + + /** + * Create product + */ + __createProduct(): void {} + + /** + * Toggle product details + * + * @param productId + */ + __toggleDetails(productId: string): void {} + + /** + * Track by function for ngFor loops + * + * @param index + * @param item + */ + __trackByFn(index: number, item: any): any { + return item.id || index; + } +} diff --git a/src/app/modules/admin/member/coupon/coupon.module.ts b/src/app/modules/admin/member/coupon/coupon.module.ts new file mode 100644 index 0000000..90ef0ee --- /dev/null +++ b/src/app/modules/admin/member/coupon/coupon.module.ts @@ -0,0 +1,50 @@ +import { NgModule } from '@angular/core'; +import { RouterModule } from '@angular/router'; + +import { MatButtonModule } from '@angular/material/button'; +import { MatFormFieldModule } from '@angular/material/form-field'; +import { MatIconModule } from '@angular/material/icon'; +import { MatInputModule } from '@angular/material/input'; +import { MatPaginatorModule } from '@angular/material/paginator'; +import { MatProgressBarModule } from '@angular/material/progress-bar'; +import { MatRippleModule } from '@angular/material/core'; +import { MatSortModule } from '@angular/material/sort'; +import { MatSelectModule } from '@angular/material/select'; +import { MatTooltipModule } from '@angular/material/tooltip'; +import { MatGridListModule } from '@angular/material/grid-list'; +import { MatSlideToggleModule } from '@angular/material/slide-toggle'; +import { MatRadioModule } from '@angular/material/radio'; +import { MatCheckboxModule } from '@angular/material/checkbox'; + +import { TranslocoModule } from '@ngneat/transloco'; + +import { SharedModule } from 'app/shared/shared.module'; + +import { COMPONENTS } from './components'; + +import { CouponRoutes } from './coupon.routing'; + +@NgModule({ + declarations: [COMPONENTS], + imports: [ + TranslocoModule, + SharedModule, + RouterModule.forChild(CouponRoutes), + + MatButtonModule, + MatFormFieldModule, + MatIconModule, + MatInputModule, + MatPaginatorModule, + MatProgressBarModule, + MatRippleModule, + MatSortModule, + MatSelectModule, + MatTooltipModule, + MatGridListModule, + MatSlideToggleModule, + MatRadioModule, + MatCheckboxModule, + ], +}) +export class CouponModule {} diff --git a/src/app/modules/admin/member/coupon/coupon.routing.ts b/src/app/modules/admin/member/coupon/coupon.routing.ts new file mode 100644 index 0000000..5d8d8b2 --- /dev/null +++ b/src/app/modules/admin/member/coupon/coupon.routing.ts @@ -0,0 +1,24 @@ +import { Route } from '@angular/router'; + +import { ListComponent } from './components/list.component'; +import { ViewComponent } from '../user/components/view.component'; + +import { CouponsResolver } from './resolvers/coupon.resolver'; +import { UserResolver } from '../user/resolvers/user.resolver'; + +export const CouponRoutes: Route[] = [ + { + path: '', + component: ListComponent, + resolve: { + Coupons: CouponsResolver, + }, + }, + { + path: ':id', + component: ViewComponent, + resolve: { + users: UserResolver, + }, + }, +]; diff --git a/src/app/modules/admin/member/coupon/models/coupon-pagination.ts b/src/app/modules/admin/member/coupon/models/coupon-pagination.ts new file mode 100644 index 0000000..c0d5939 --- /dev/null +++ b/src/app/modules/admin/member/coupon/models/coupon-pagination.ts @@ -0,0 +1,8 @@ +export interface CouponPagination { + length: number; + size: number; + page: number; + lastPage: number; + startIndex: number; + endIndex: number; +} diff --git a/src/app/modules/admin/member/coupon/models/coupon.ts b/src/app/modules/admin/member/coupon/models/coupon.ts new file mode 100644 index 0000000..d21561e --- /dev/null +++ b/src/app/modules/admin/member/coupon/models/coupon.ts @@ -0,0 +1,29 @@ +export interface Coupon { + id?: string; + totalPartnerCount?: number; + totalHoldingMoney?: number; + totalComp?: number; + total?: number; + branchCount?: number; + divisionCount?: number; + officeCount?: number; + storeCount?: number; + memberCount?: number; + nickname?: string; + accountHolder?: string; + phoneNumber?: string; + calculateType?: string; + ownCash?: number; + ownComp?: number; + ownCoupon?: number; + gameMoney?: number; + todayComp?: number; + totalDeposit?: number; + totalWithdraw?: number; + balance?: number; + registDate?: string; + finalSigninDate?: string; + ip?: string; + state?: string; + note?: string; +} diff --git a/src/app/modules/admin/member/coupon/resolvers/coupon.resolver.ts b/src/app/modules/admin/member/coupon/resolvers/coupon.resolver.ts new file mode 100644 index 0000000..eee8669 --- /dev/null +++ b/src/app/modules/admin/member/coupon/resolvers/coupon.resolver.ts @@ -0,0 +1,84 @@ +import { Injectable } from '@angular/core'; +import { + ActivatedRouteSnapshot, + Resolve, + Router, + RouterStateSnapshot, +} from '@angular/router'; +import { catchError, Observable, throwError } from 'rxjs'; + +import { Coupon } from '../models/coupon'; +import { CouponPagination } from '../models/coupon-pagination'; +import { CouponService } from '../services/coupon.service'; + +@Injectable({ + providedIn: 'root', +}) +export class CouponResolver implements Resolve { + /** + * Constructor + */ + constructor(private _couponService: CouponService, private _router: Router) {} + + // ----------------------------------------------------------------------------------------------------- + // @ Public methods + // ----------------------------------------------------------------------------------------------------- + + /** + * Resolver + * + * @param route + * @param state + */ + resolve( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot + ): Observable { + return this._couponService.getCouponById(route.paramMap.get('id')).pipe( + // Error here means the requested product is not available + catchError((error) => { + // Log the error + console.error(error); + + // Get the parent url + const parentUrl = state.url.split('/').slice(0, -1).join('/'); + + // Navigate to there + this._router.navigateByUrl(parentUrl); + + // Throw an error + return throwError(error); + }) + ); + } +} + +@Injectable({ + providedIn: 'root', +}) +export class CouponsResolver implements Resolve { + /** + * Constructor + */ + constructor(private _couponService: CouponService) {} + + // ----------------------------------------------------------------------------------------------------- + // @ Public methods + // ----------------------------------------------------------------------------------------------------- + + /** + * Resolver + * + * @param route + * @param state + */ + resolve( + route: ActivatedRouteSnapshot, + state: RouterStateSnapshot + ): Observable<{ + pagination: CouponPagination; + coupons: Coupon[]; + }> { + return this._couponService.getCoupons(); + } +} diff --git a/src/app/modules/admin/member/coupon/services/coupon.service.ts b/src/app/modules/admin/member/coupon/services/coupon.service.ts new file mode 100644 index 0000000..f3d4bc1 --- /dev/null +++ b/src/app/modules/admin/member/coupon/services/coupon.service.ts @@ -0,0 +1,153 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { + BehaviorSubject, + filter, + map, + Observable, + of, + switchMap, + take, + tap, + throwError, +} from 'rxjs'; + +import { Coupon } from '../models/coupon'; +import { CouponPagination } from '../models/coupon-pagination'; + +@Injectable({ + providedIn: 'root', +}) +export class CouponService { + // Private + private __pagination = new BehaviorSubject( + undefined + ); + private __coupon = new BehaviorSubject(undefined); + private __coupons = new BehaviorSubject(undefined); + + /** + * Constructor + */ + constructor(private _httpClient: HttpClient) {} + + // ----------------------------------------------------------------------------------------------------- + // @ Accessors + // ----------------------------------------------------------------------------------------------------- + + /** + * Getter for pagination + */ + get pagination$(): Observable { + return this.__pagination.asObservable(); + } + + /** + * Getter for coupon + */ + get coupon$(): Observable { + return this.__coupon.asObservable(); + } + + /** + * Getter for coupons + */ + get coupons$(): Observable { + return this.__coupons.asObservable(); + } + + // ----------------------------------------------------------------------------------------------------- + // @ Public methods + // ----------------------------------------------------------------------------------------------------- + + /** + * Get coupons + * + * + * @param page + * @param size + * @param sort + * @param order + * @param search + */ + getCoupons( + page: number = 0, + size: number = 10, + sort: string = 'name', + order: 'asc' | 'desc' | '' = 'asc', + search: string = '' + ): Observable<{ + pagination: CouponPagination; + coupons: Coupon[]; + }> { + return this._httpClient + .get<{ + pagination: CouponPagination; + coupons: Coupon[]; + }>('api/apps/member/coupon/coupons', { + params: { + page: '' + page, + size: '' + size, + sort, + order, + search, + }, + }) + .pipe( + tap((response) => { + this.__pagination.next(response.pagination); + this.__coupons.next(response.coupons); + }) + ); + } + + /** + * Get product by id + */ + getCouponById(id: string | null): Observable { + return this.__coupons.pipe( + take(1), + map((coupons) => { + // Find the product + const coupon = coupons?.find((item) => item.id === id) || undefined; + + // Update the product + this.__coupon.next(coupon); + + // Return the product + return coupon; + }), + switchMap((product) => { + if (!product) { + return throwError('Could not found product with id of ' + id + '!'); + } + + return of(product); + }) + ); + } + + /** + * Create product + */ + createCoupon(): Observable { + return this.coupons$.pipe( + take(1), + switchMap((coupons) => + this._httpClient + .post('api/apps/member/coupon/product', {}) + .pipe( + map((newCoupon) => { + // Update the coupons with the new product + if (!!coupons) { + this.__coupons.next([newCoupon, ...coupons]); + } + + // Return the new product + return newCoupon; + }) + ) + ) + ); + } +} diff --git a/src/assets/i18n/en.json b/src/assets/i18n/en.json index e307559..497e538 100644 --- a/src/assets/i18n/en.json +++ b/src/assets/i18n/en.json @@ -13,6 +13,7 @@ "Partner Office": "Partner Office", "Partner Store": "Partner Store", "Partner Recommendation": "Partner Recommendation", + "Coupon": "Coupon", "Analytics": "Analytics", "Deposit": "Deposit", "Withdraw": "Withdraw", diff --git a/src/assets/i18n/ko.json b/src/assets/i18n/ko.json index e1c2b97..3a8c04e 100644 --- a/src/assets/i18n/ko.json +++ b/src/assets/i18n/ko.json @@ -13,6 +13,7 @@ "Partner Office": "총판", "Partner Store": "매장", "Partner Recommendation": "추천코드등록", + "Coupon": "쿠폰발행리스트", "Analytics": "Analytics", "Deposit": "입금관리", "Withdraw": "출금관리",