-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocations.ts
More file actions
136 lines (125 loc) · 2.88 KB
/
locations.ts
File metadata and controls
136 lines (125 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
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
import prisma from '@/server/db'
import { logger } from '@/server/logger'
// Function definition with types
async function createLocation(
subscriptionId: number,
locationData: LocationData,
geoAddressData: GeoAddressData
): Promise<void> {
// Destructure locationData
const {
coords: { accuracy, altitude, altitudeAccuracy, heading, latitude, longitude, speed },
mocked,
timestamp,
} = locationData
// Destructure geoAddressData
const {
city,
country,
district,
formattedAddress,
isoCountryCode,
name,
postalCode,
region,
street,
streetNumber,
subregion,
timezone,
} = geoAddressData
// Insert query
const location = await prisma.location.create({
data: {
accuracy,
altitude,
altitudeAccuracy,
heading,
latitude,
longitude,
speed,
mocked,
timestamp,
city,
country,
district,
formattedAddress,
isoCountryCode,
name,
postalCode,
region,
street,
streetNumber,
subregion,
timezone,
ipAddress: '127.0.0.1', // Replace with the actual IP address
// Use the relation for subscription
subscription: {
connect: { id: subscriptionId }, // Connect to an existing subscription by ID
},
},
})
logger.info('Location created:', location)
}
// Example usage
const subscriptionId = 1 // Replace with the actual subscription ID
const locationData: LocationData = {
coords: {
accuracy: 100,
altitude: 236.60000610351562,
altitudeAccuracy: 100,
heading: 0,
latitude: 44.9560359,
longitude: -93.2941526,
speed: 0,
},
mocked: false,
timestamp: 1737836129669,
}
const geoAddressData: GeoAddressData = {
city: 'Minneapolis',
country: 'United States',
district: null,
formattedAddress: '2543 Emerson Ave S, Minneapolis, MN 55405, USA',
isoCountryCode: 'US',
name: '2543',
postalCode: '55405',
region: 'Minnesota',
street: 'Emerson Avenue South',
streetNumber: '2543',
subregion: 'Hennepin County',
timezone: null,
}
createLocation(subscriptionId, locationData, geoAddressData)
.catch((e) => logger.error(e))
.finally(async () => {
await prisma.$disconnect()
})
// Define the types/interfaces
interface Coords {
accuracy: number
altitude: number
altitudeAccuracy: number
heading: number
latitude: number
longitude: number
speed: number
}
interface LocationData {
coords: Coords
mocked: boolean
timestamp: number // Use number to match the BigInt-like behavior in JS
}
interface GeoAddressData {
city: string | null
country: string | null
district: string | null
formattedAddress: string | null
isoCountryCode: string | null
name: string | null
postalCode: string | null
region: string | null
street: string | null
streetNumber: string | null
subregion: string | null
timezone: string | null
}