Passed
Branch test/scrutinizer-config-backen... (e3cdc3)
by Karl
06:16
created

BicycleSeeder.run   C

Complexity

Conditions 5

Size

Total Lines 183
Code Lines 140

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 17.3024

Importance

Changes 0
Metric Value
cc 5
eloc 140
dl 0
loc 183
ccs 4
cts 19
cp 0.2105
crap 17.3024
rs 6.5333
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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