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
|
# File 'app/controllers/sequence_generator/sequences_controller.rb', line 28
def index
sequence = Sequence.where(purpose: params[:purpose], scope: params[:scope])
.where('valid_from <= ? AND valid_till >= ?', Time.now, Time.now).first
if sequence
sequence_number = sequence.generate_sequence_number
render json: { sequence_number: sequence_number, purpose: params[:purpose],
scope: params[:scope] }, status: :ok
else
original_sequence = Sequence.where(purpose: params[:purpose], scope: params[:scope]).last
if original_sequence.nil?
api_error(status: :unprocessable_entity,
message: 'Sequence is not Created')
else
valid_from = original_sequence.valid_from
valid_till = original_sequence.valid_till
new_start_at = original_sequence.sequential_id
difference = (valid_till - valid_from).to_i
new_valid_from = Date.today
new_valid_till = new_valid_from + difference
if original_sequence.reset_from_next_year
sequence = Sequence.create!(original_sequence.as_json.except('id', 'start_at',
'valid_from', 'valid_till',
'sequential_id',
'created_at', 'updated_at')
.merge!(start_at: 1,
valid_from: new_valid_from,
valid_till: new_valid_till,
sequential_id: 0))
else
sequence = Sequence.create!(original_sequence.as_json.except('id', 'start_at',
'valid_from', 'valid_till',
'sequential_id',
'created_at', 'updated_at')
.merge!(start_at: new_start_at,
valid_from: new_valid_from,
valid_till: new_valid_till,
sequential_id: new_start_at))
end
sequence_number = sequence.generate_sequence_number
render json: { sequence_number: sequence_number, purpose: params[:purpose],
scope: params[:scope] }, status: :ok
end
end
end
|