1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00

Merge branch 'dev' for release 5.4.25

This commit is contained in:
Du Peng 2022-10-19 18:20:00 +02:00
commit 24a47acec7
11 changed files with 250 additions and 4 deletions

View File

@ -1,5 +1,10 @@
# Changelog Fab-manager
## v5.4.25 2022 October 19
- Fix a bug: unable apply a coupon if this coupon has used by an user removed
- Improved automated test on prepaid pack
## v5.4.24 2022 October 14
- Fix a bug: unable debit hours of prepaid pack after a reservation of machine

View File

@ -82,7 +82,8 @@ class Coupon < ApplicationRecord
end
def users
invoices.map(&:user)
# compact to user removed
invoices.map(&:user).compact
end
def users_ids
@ -104,5 +105,4 @@ class Coupon < ApplicationRecord
def delete_gateway_coupon
PaymentGatewayService.new.delete_coupon(id)
end
end

View File

@ -38,7 +38,7 @@ class PrepaidPackService
# total number of minutes in the reservation's slots
slots_minutes = reservation.slots.map do |slot|
(slot.end_at.to_time - slot.start_at.to_time) / SECONDS_PER_MINUTE
(slot.end_at.to_time - slot.start_at.to_time) / 60.0
end
reservation_minutes = slots_minutes.reduce(:+) || 0

View File

@ -204,5 +204,79 @@ namespace :fablab do
ip.update_attribute('email', ip.user.email)
end
end
desc '[release 5.4.24] fix prepaid pack hours dont count down after a reservation of machine'
task :prepaid_pack_count_down, %i[start_date end_date] => :environment do |_task, args|
# set start date to the date of deployment of v5.4.13 that product the bug
start_date = DateTime.parse('2022-07-28T10:00:00+02:00')
if args.start_date
begin
start_date = DateTime.parse(args.start_date)
rescue ArgumentError => e
raise e
end
end
# set end date to the date of deployment of v5.4.24 after fix the bug
end_date = DateTime.parse('2022-10-14T18:40:00+02:00')
if args.end_date
begin
end_date = DateTime.parse(args.end_date)
rescue ArgumentError => e
raise e
end
end
# find all machines that has prepaid pack
machine_ids = PrepaidPack.where(disabled: nil).all.map(&:priceable_id).uniq
# find all memders that bought a prepaid pack
statistic_profile_ids = StatisticProfilePrepaidPack.all.map(&:statistic_profile_id).uniq
# find the reservations that use prepaid pack by machine_ids, members and preriod
reservations = Reservation.where(reservable_type: 'Machine', reservable_id: machine_ids, statistic_profile_id: statistic_profile_ids,
created_at: start_date..end_date).order(statistic_profile_id: 'ASC', created_at: 'ASC')
infos = []
reservations.each do |reservation|
# find pack by pack's created_at before reservation's create_at and pack's expries_at before start_date
packs = StatisticProfilePrepaidPack
.includes(:prepaid_pack)
.references(:prepaid_packs)
.where('prepaid_packs.priceable_id = ?', reservation.reservable.id)
.where('prepaid_packs.priceable_type = ?', reservation.reservable.class.name)
.where(statistic_profile_id: reservation.statistic_profile_id)
.where('statistic_profile_prepaid_packs.created_at <= ?', reservation.created_at)
.where('expires_at is NULL or expires_at > ?', start_date)
.order(created_at: 'ASC')
# passe reservation if cannot find any pack
next unless packs.length > 0
user = reservation.statistic_profile.user
pack = packs.last
slots_minutes = reservation.slots.map do |slot|
(slot.end_at.to_time - slot.start_at.to_time) / 60.0
end
# get reservation total minutes
reservation_minutes = slots_minutes.reduce(:+) || 0
info = {
user: "#{user.profile.full_name} - #{user.email}",
reservation: "Reservation #{reservation.original_invoice.reference} for the machine #{reservation.reservable.name} by #{reservation_minutes / 60.0} hours at #{I18n.l(reservation.created_at.to_date)}",
pack_before: "Prepaid pack of hours has used #{pack.minutes_used / 60.0} hours / #{pack.prepaid_pack.minutes / 60.0} hours"
}
if pack.minutes_used == pack.prepaid_pack.minutes && pack.updated_at > start_date
info[:pack_after] = 'Reservation minutes is exceed prepaid pack of hours'
infos.push(info)
elsif pack.minutes_used < pack.prepaid_pack.minutes
PrepaidPackService.update_user_minutes(user, reservation)
pack.reload
info[:pack_after] = "Prepaid pack of hours used #{pack.minutes_used / 60.0} hours after paid this reservation"
infos.push(info)
end
end
infos.each do |i|
puts i
end
end
end
end

View File

@ -1,6 +1,6 @@
{
"name": "fab-manager",
"version": "5.4.24",
"version": "5.4.25",
"description": "Fab-manager is the FabLab management solution. It provides a comprehensive, web-based, open-source tool to simplify your administrative tasks and your marker's projects.",
"keywords": [
"fablab",

View File

@ -74,3 +74,15 @@ address_6:
placeable_type: InvoicingProfile
created_at: 2016-08-02 11:16:24.412236000 Z
updated_at: 2016-08-02 11:16:24.412236000 Z
address_7:
id: 7
address: 14 rue du Général Choucroute, 44000 NANTES
street_number:
route:
locality:
postal_code:
placeable_id: 10
placeable_type: InvoicingProfile
created_at: 2016-08-02 11:16:24.412236000 Z
updated_at: 2016-08-02 11:16:24.412236000 Z

15
test/fixtures/prepaid_packs.yml vendored Normal file
View File

@ -0,0 +1,15 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
prepaid_pack_1:
id: 1
priceable_type: Machine
priceable_id: 1
group_id: 1
amount: 1000
minutes: 600
validity_interval: month
validity_count: 12

View File

@ -0,0 +1,13 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
statistic_profile_prepaid_pack_1:
prepaid_pack_id: 1
statistic_profile_id: 10
minutes_used: 600
expires_at: <%= 11.month.from_now.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>
statistic_profile_prepaid_pack_2:
prepaid_pack_id: 1
statistic_profile_id: 10
minutes_used: 0
expires_at: <%= 12.month.from_now.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %>

View File

@ -0,0 +1,76 @@
# frozen_string_literal: true
require 'test_helper'
module Reservations; end
class Reservations::PayWithPrepaidPackTest < ActionDispatch::IntegrationTest
setup do
@acamus = User.find_by(username: 'acamus')
end
test 'user reserves a machine and pay by prepaid pack with success' do
login_as(@acamus, scope: :user)
machine = Machine.first
availability = machine.availabilities.first
reservations_count = Reservation.count
invoice_count = Invoice.count
invoice_items_count = InvoiceItem.count
VCR.use_cassette('reservations_create_for_machine_with_prepaid_pack_success') do
post '/api/local_payment/confirm_payment',
params: {
items: [
{
reservation: {
reservable_id: machine.id,
reservable_type: machine.class.name,
slots_reservations_attributes: [
{
slot_id: availability.slots.first.id
}
]
}
}
]
}.to_json, headers: default_headers
end
# general assertions
assert_equal 201, response.status
assert_equal reservations_count + 1, Reservation.count
assert_equal invoice_count + 1, Invoice.count
assert_equal invoice_items_count + 1, InvoiceItem.count
# reservation assertions
reservation = Reservation.last
assert reservation.original_invoice
assert_equal 1, reservation.original_invoice.invoice_items.count
# invoice_items assertions
invoice_item = InvoiceItem.last
assert_equal invoice_item.amount, 0
assert invoice_item.check_footprint
# invoice assertions
item = InvoiceItem.find_by(object: reservation)
invoice = item.invoice
assert_invoice_pdf invoice
assert_not_nil invoice.debug_footprint
assert invoice.payment_gateway_object.blank?
assert_not invoice.total.blank?
assert invoice.check_footprint
# notification
assert_not_empty Notification.where(attached_object: reservation)
# prepaid pack
minutes_available = PrepaidPackService.minutes_available(@acamus, machine)
assert_equal minutes_available, 540
end
end

View File

@ -0,0 +1,11 @@
require 'test_helper'
class StatisticProfilePrepaidPackTest < ActiveSupport::TestCase
test 'coupon have a expries date' do
prepaid_pack = PrepaidPack.first
user = User.find_by(username: 'jdupond')
p = StatisticProfilePrepaidPack.create!(prepaid_pack: prepaid_pack, statistic_profile: user.statistic_profile)
expires_at = DateTime.current + 12.months
assert p.expires_at.strftime('%Y-%m-%d'), expires_at.strftime('%Y-%m-%d')
end
end

View File

@ -0,0 +1,40 @@
# frozen_string_literal: true
require 'test_helper'
class PrepaidPackServiceTest < ActiveSupport::TestCase
setup do
@acamus = User.find_by(username: 'acamus')
@machine = Machine.first
end
test 'get user packs' do
packs = PrepaidPackService.user_packs(@acamus, @machine)
p = StatisticProfilePrepaidPack.where(statistic_profile_id: @acamus.statistic_profile.id)
assert_not_empty packs
assert_equal packs.length, 1
assert_equal p.length, 2
assert_equal packs.first.id, p.last.id
end
test 'total number of prepaid minutes available' do
minutes_available = PrepaidPackService.minutes_available(@acamus, @machine)
assert_equal minutes_available, 600
end
test 'update user pack minutes' do
availabilities_service = Availabilities::AvailabilitiesService.new(@acamus)
slots = availabilities_service.machines([@machine], @acamus, { start: DateTime.now, end: 1.day.from_now })
reservation = Reservation.create(
reservable_id: @machine.id,
reservable_type: Machine.name,
slots: [slots[0], slots[1]],
statistic_profile_id: @acamus.statistic_profile.id
)
PrepaidPackService.update_user_minutes(@acamus, reservation)
minutes_available = PrepaidPackService.minutes_available(@acamus, @machine)
assert_equal minutes_available, 480
end
end