EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
stores.ts
Go to the documentation of this file.
1/**
2 * Store Configuration
3 * Defines all available stores in the Cartridge World network
4 */
5
6/**
7 * Get the base domain for store URLs from environment variable
8 * Falls back to cwanz.online if not set
9 */
10export function getStoreDomain(): string {
11 return process.env.NEXT_PUBLIC_FIELDPINE_STORE_DOMAIN ||
12 process.env.FIELDPINE_STORE_DOMAIN ||
13 'cwanz.online';
14}
15
16/**
17 * Build a store URL from subdomain and domain
18 * @example buildStoreUrl('cowra') => 'https://cowra.cwanz.online'
19 */
20export function buildStoreUrl(subdomain: string): string {
21 const domain = getStoreDomain();
22 return `https://${subdomain}.${domain}`;
23}
24
25export type StoreType = 'management' | 'store';
26
27export interface Store {
28 id: string;
29 name: string;
30 url: string;
31 type: StoreType;
32 location?: string;
33 description?: string;
34 manifestId?: string; // Original numeric ID from Fieldpine manifest
35}
36
37/**
38 * Mapping between Fieldpine manifest IDs and friendly authentication IDs
39 * This allows us to use friendly IDs (like 'iig', 'cowra') in authentication
40 * while maintaining compatibility with the Fieldpine numeric IDs
41 */
42export const STORE_ID_MAPPING: Record<string, string> = {
43 '8': 'iig', // IIG Management Portal
44 '1': 'cowra', // Cowra Store
45 '4': 'griffith', // Griffith Store
46 '11': 'murwillumbah', // Murwillumbah Store
47};
48
49/**
50 * Reverse mapping: friendly ID to manifest ID
51 */
52export const FRIENDLY_TO_MANIFEST_ID: Record<string, string> = {
53 'iig': '8',
54 'cowra': '1',
55 'griffith': '4',
56 'murwillumbah': '11',
57};
58
59/**
60 * Transform a store from the manifest format to our authentication format
61 */
62export function transformManifestStore(manifestStore: any): Store | null {
63 const friendlyId = STORE_ID_MAPPING[manifestStore.id];
64
65 // Only include stores we want to authenticate against
66 if (!friendlyId) {
67 return null;
68 }
69
70 return {
71 id: friendlyId,
72 name: manifestStore.name.replace(' Agency', ''), // Clean up name
73 url: manifestStore.url,
74 type: friendlyId === 'iig' ? 'management' : 'store',
75 location: manifestStore.email ? undefined : manifestStore.location,
76 description: manifestStore.name,
77 manifestId: manifestStore.id,
78 };
79}
80
81/**
82 * Static fallback stores for authentication
83 * Used when manifest is unavailable
84 * URLs are built dynamically from environment variable
85 */
86export const stores: Store[] = [
87 {
88 id: 'iig',
89 name: 'IIG',
90 url: buildStoreUrl('iig'),
91 type: 'management',
92 description: 'Independent Innovative Group - Central Management Portal',
93 manifestId: '8',
94 },
95 {
96 id: 'cowra',
97 name: 'Cowra',
98 url: buildStoreUrl('cowra'),
99 type: 'store',
100 location: 'Cowra, NSW',
101 description: 'Cartridge World Cowra Store',
102 manifestId: '1',
103 },
104 {
105 id: 'griffith',
106 name: 'Griffith',
107 url: buildStoreUrl('griffith'),
108 type: 'store',
109 location: 'Griffith, NSW',
110 description: 'Cartridge World Griffith Store',
111 manifestId: '4',
112 },
113 {
114 id: 'murwillumbah',
115 name: 'Murwillumbah',
116 url: buildStoreUrl('murwillumbah'),
117 type: 'store',
118 location: 'Murwillumbah, NSW',
119 description: 'Cartridge World Murwillumbah Store',
120 manifestId: '11',
121 },
122];
123
124/**
125 * Get store by ID
126 */
127export function getStoreById(id: string): Store | undefined {
128 return stores.find((store) => store.id === id);
129}
130
131/**
132 * Get all stores of a specific type
133 */
134export function getStoresByType(type: StoreType): Store[] {
135 return stores.filter((store) => store.type === type);
136}
137
138/**
139 * Get the management portal
140 */
141export function getManagementPortal(): Store {
142 const portal = stores.find((store) => store.type === 'management');
143 if (!portal) {
144 throw new Error('Management portal not found in store configuration');
145 }
146 return portal;
147}
148
149/**
150 * Get all retail stores (excluding management portal)
151 */
152export function getRetailStores(): Store[] {
153 return getStoresByType('store');
154}
155
156/**
157 * Check if a store ID is valid
158 */
159export function isValidStoreId(id: string): boolean {
160 return stores.some((store) => store.id === id);
161}
162
163/**
164 * Get store by URL
165 */
166export function getStoreByUrl(url: string): Store | undefined {
167 return stores.find((store) => store.url === url);
168}