Passed
Pull Request — development (#102)
by Karl
12:26
created

backend/src/database/seeds/bicycles-data.seed.ts   A

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 193
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 146
dl 0
loc 193
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 5
mnd 4
bc 4
fnc 1
bpm 4
cpm 5
noi 1

1 Function

Rating   Name   Duplication   Size   Complexity  
C BicycleSeeder.run 0 183 5
1
import { DataSource } from 'typeorm';
2 7
import { Bicycle } from '../../bicycles/entities/bicycle.entity';
3 7
import { City } from '../../cities/entities/city.entity';
4 7
import { CityName } from '../../cities/types/city.enum';
5
6 7
export default class BicycleSeeder {
7
  async run(connection: DataSource): Promise<void> {
8 9
    if (true) {//process.env.NODE_ENV !== 'production') {
0 ignored issues
show
introduced by
Insert ⏎······
Loading history...
9 9
      const bicycleRepo = connection.getRepository(Bicycle);
10 9
      const cityRepo = connection.getRepository(City);
11
12
      // keeping debug logs for now
13 9
      let goteborg = await cityRepo.findOne({ where: { name: CityName.Göteborg } });
14 9
      if (!goteborg) {
15
        // console.log('Creating Göteborg...');
16 5
        goteborg = cityRepo.create({ name: CityName.Göteborg });
17 5
        await cityRepo.save(goteborg);
18
        // console.log('Göteborg created with ID:', goteborg.id);
19
      }
20
21 9
      let karlshamn = await cityRepo.findOne({ where: { name: CityName.Karlshamn } });
22 9
      if (!karlshamn) {
23
        // console.log('Creating Karlshamn...');
24 5
        karlshamn = cityRepo.create({ name: CityName.Karlshamn });
25 5
        await cityRepo.save(karlshamn);
26
        // console.log('Karlshamn created with ID:', karlshamn.id);
27
      }
28
29 9
      let jonkoping = await cityRepo.findOne({ where: { name: CityName.Jönköping } });
30 9
      if (!jonkoping) {
31
        // console.log('Creating Jönköping...');
32 5
        jonkoping = cityRepo.create({ name: CityName.Jönköping });
33 5
        await cityRepo.save(jonkoping);
34
        // console.log('Jönköping created with ID:', jonkoping.id);
35
      }
36
37
      // const cities = await cityRepo.find();
38
      // console.log('Available cities:', cities);
39
      // console.log(goteborg)
40
      // console.log(karlshamn)
41
42
      // Add example bicycles
43
      // Göteborg bikes
44
      type BicycleStatus = 'Available' | 'Service' | 'Rented';
45
46 9
      const goteborgBikes = [
47
        // Available bikes
48
        {
49
          batteryLevel: 95,
50
          latitude: 57.7089,
51
          longitude: 11.9726,
52
          status: 'Available' as BicycleStatus,
53
          city: goteborg,
54
        },
55
        {
56
          batteryLevel: 88,
57
          latitude: 57.7095,
58
          longitude: 11.9689,
59
          status: 'Available' as BicycleStatus,
60
          city: goteborg,
61
        },
62
        {
63
          batteryLevel: 92,
64
          latitude: 57.7066,
65
          longitude: 11.9384,
66
          status: 'Available' as BicycleStatus,
67
          city: goteborg,
68
        },
69
        // Service bikes
70
        {
71
          batteryLevel: 15,
72
          latitude: 57.7,
73
          longitude: 11.9753,
74
          status: 'Service' as BicycleStatus,
75
          city: goteborg,
76
        },
77
        {
78
          batteryLevel: 5,
79
          latitude: 57.7048,
80
          longitude: 11.9757,
81
          status: 'Service' as BicycleStatus,
82
          city: goteborg,
83
        },
84
        {
85
          batteryLevel: 25,
86
          latitude: 57.7001,
87
          longitude: 11.9765,
88
          status: 'Service' as BicycleStatus,
89
          city: goteborg,
90
        },
91
      ];
92
93
      // Karlshamn bikes
94 9
      const karlshamnBikes = [
95
        // Available bikes
96
        {
97
          batteryLevel: 90,
98
          latitude: 56.1708,
99
          longitude: 14.8631,
100
          status: 'Available' as BicycleStatus,
101
          city: karlshamn,
102
        },
103
        {
104
          batteryLevel: 85,
105
          latitude: 56.1657,
106
          longitude: 14.8607,
107
          status: 'Available' as BicycleStatus,
108
          city: karlshamn,
109
        },
110
        {
111
          batteryLevel: 93,
112
          latitude: 56.1695,
113
          longitude: 14.8598,
114
          status: 'Available' as BicycleStatus,
115
          city: karlshamn,
116
        },
117
        // Service bikes
118
        {
119
          batteryLevel: 12,
120
          latitude: 56.1712,
121
          longitude: 14.8637,
122
          status: 'Service' as BicycleStatus,
123
          city: karlshamn,
124
        },
125
        {
126
          batteryLevel: 8,
127
          latitude: 56.1659,
128
          longitude: 14.8612,
129
          status: 'Service' as BicycleStatus,
130
          city: karlshamn,
131
        },
132
        {
133
          batteryLevel: 20,
134
          latitude: 56.1691,
135
          longitude: 14.8608,
136
          status: 'Service' as BicycleStatus,
137
          city: karlshamn,
138
        },
139
      ];
140
141
      // Jönköping bikes
142 9
      const jonkopingBikes = [
143
        // Available bikes
144
        {
145
          batteryLevel: 97,
146
          latitude: 57.7741,
147
          longitude: 14.2031,
148
          status: 'Available' as BicycleStatus,
149
          city: jonkoping,
150
        },
151
        {
152
          batteryLevel: 89,
153
          latitude: 57.7843,
154
          longitude: 14.1617,
155
          status: 'Available' as BicycleStatus,
156
          city: jonkoping,
157
        },
158
        {
159
          batteryLevel: 94,
160
          latitude: 57.7819,
161
          longitude: 14.1556,
162
          status: 'Available' as BicycleStatus,
163
          city: jonkoping,
164
        },
165
        // Service bikes
166
        {
167
          batteryLevel: 10,
168
          latitude: 57.7701,
169
          longitude: 14.2173,
170
          status: 'Service' as BicycleStatus,
171
          city: jonkoping,
172
        },
173
        {
174
          batteryLevel: 18,
175
          latitude: 57.7706,
176
          longitude: 14.2183,
177
          status: 'Service' as BicycleStatus,
178
          city: jonkoping,
179
        },
180
        {
181
          batteryLevel: 7,
182
          latitude: 57.7696,
183
          longitude: 14.2183,
184
          status: 'Service' as BicycleStatus,
185
          city: jonkoping,
186
        },
187
      ];
188
189 9
      await bicycleRepo.save([...goteborgBikes, ...karlshamnBikes, ...jonkopingBikes]);
190
    }
191
  }
192
}
193