]> nos-oignons.net Git - gestion-adh.git/blob - lib/nos_oignons.rb
ff387d1af21b6ca32b2dc33deb0a09e70eaf7fb6
[gestion-adh.git] / lib / nos_oignons.rb
1 #-*- coding: utf-8 -*-
2
3 require 'nos_oignons/git'
4 require 'nos_oignons/mailman'
5 require 'nos_oignons/member'
6 require 'nos_oignons/reminder'
7 require 'nos_oignons/reminder_db'
8
9 module NosOignons
10   BOARD_EMAIL = 'ca@nos-oignons.net'
11   MEMBER_MAILING_LIST = 'ag'
12
13   # The following class methods are all meant to be called as command-line scripts
14   class << self
15     def list_emails!
16       NosOignons::Member.all.each do |member|
17         if member.up_to_date?
18           puts member.email
19         end
20       end
21     end
22
23     def update_ag_subscribers!
24       list = NosOignons::MEMBER_MAILING_LIST
25
26       current_emails = NosOignons::Mailman.list_members(list)
27       uptodate_emails = NosOignons::Member.all.select(&:up_to_date?).collect(&:email)
28
29       emails_to_add = uptodate_emails - current_emails
30       NosOignons::Mailman.add_members(list, emails_to_add) unless emails_to_add.empty?
31       emails_to_remove = current_emails - uptodate_emails
32       NosOignons::Mailman.remove_members(list, emails_to_remove) unless emails_to_remove.empty?
33     end
34
35     def send_membership_reminders!
36       today = Time.now.to_date
37       NosOignons::Member.all.select(&:up_to_date?).each do |member|
38         NosOignons::Reminder.all.sort_by(&:days).reverse.each do |reminder|
39           anniversary = Time.new(today.year, member.joined_on.month,
40                                  member.joined_on.day).to_date
41
42           next if member.membership_fee_paid_on > anniversary
43           next if member.membership_fee_paid_on > today - reminder.days
44           next if anniversary > today - reminder.days
45           next if member.reminded_on && member.reminded_on >= today
46
47           member.remind(reminder)
48           break
49         end
50       end
51     end
52
53     def pre_commit_hook!
54       if system('git rev-parse --quiet --verify HEAD >/dev/null')
55         against = 'HEAD'
56       else
57         # Initial commit: diff against an empty tree object
58         against = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
59       end
60
61       IO.popen(['git', 'diff-index', '--cached', '--name-status', against]) do |io|
62         NosOignons::Git.handle_modified_files(io) do |file|
63           next unless file.start_with?("#{NosOignons::MEMBERS_DB_DIR}/")
64           begin
65             # Use empty ref to get the index
66             NosOignons::Member.read_from_git('', file)
67           rescue ArgumentError
68             $stderr.puts "Désolé : #{file} n'a pas le bon format !"
69             exit 1
70           end
71         end
72       end
73     end
74
75     def pre_receive_hook!(stdin)
76       stdin.readlines.each do |ref_line|
77         old_value, new_value, ref_name = ref_line.rstrip.split(' ', 3)
78         IO.popen(['git', 'diff', '--name-status', "#{old_value}..#{new_value}"]) do |io|
79           NosOignons::Git.handle_modified_files(io) do |file|
80             next unless file.start_with?("#{NosOignons::MEMBERS_DB_DIR}/")
81             begin
82               NosOignons::Member.read_from_git(new_value, file)
83             rescue ArgumentError
84               $stderr.puts "Désolé : #{file} n'a pas le bon format !"
85               exit 1
86             end
87           end
88         end
89       end
90     end
91   end
92 end